HandleProxy* V8EngineProxy::Compile(const uint16_t* script, uint16_t* sourceName) { HandleProxy *returnVal = nullptr; try { TryCatch __tryCatch; //__tryCatch.SetVerbose(true); if (sourceName == nullptr) sourceName = (uint16_t*)L""; auto hScript = NewUString(script); auto compiledScript = Script::Compile(hScript, NewUString(sourceName)); if (__tryCatch.HasCaught()) { returnVal = GetHandleProxy(GetErrorMessage(__tryCatch)); returnVal->_Type = JSV_CompilerError; } else { returnVal = GetHandleProxy(Handle<Value>()); returnVal->SetHandle(compiledScript); returnVal->_Value.V8String = _StringItem(this, *hScript).String; } } catch (exception ex) { returnVal = GetHandleProxy(NewString(ex.what())); returnVal->_Type = JSV_InternalError; } return returnVal; }
HandleProxy* V8EngineProxy::Execute(const uint16_t* script, uint16_t* sourceName) { HandleProxy *returnVal = nullptr; try { TryCatch __tryCatch; //__tryCatch.SetVerbose(true); if (sourceName == nullptr) sourceName = (uint16_t*)L""; auto compiledScript = Script::Compile(NewUString(script), NewUString(sourceName)); if (__tryCatch.HasCaught()) { returnVal = GetHandleProxy(GetErrorMessage(__tryCatch)); returnVal->_Type = JSV_CompilerError; } else returnVal = Execute(compiledScript); } catch (exception ex) { returnVal = GetHandleProxy(NewString(ex.what())); returnVal->_Type = JSV_InternalError; } return returnVal; }
HandleProxy* V8EngineProxy::CreateError(const uint16_t* message, JSValueType errorType) { if (errorType >= 0) throw exception("Invalid error type."); auto h = GetHandleProxy(NewUString(message)); h->_Type = errorType; return h; }
HandleProxy* V8EngineProxy::CreateArray(uint16_t** items, uint16_t length) { Local<Array> array = NewArray(length); if (items != nullptr && length > 0) for (auto i = 0; i < length; i++) array->Set(i, NewUString(items[i])); return GetHandleProxy(array); }
HandleProxy* V8EngineProxy::Call(HandleProxy *subject, const uint16_t *functionName, HandleProxy *_this, uint16_t argCount, HandleProxy** args) { if (_this == nullptr) _this = subject; // (assume the subject is also "this" if not given) auto hThis = _this->Handle(); if (hThis.IsEmpty() || !hThis->IsObject()) throw exception("Call: The target instance handle ('this') does not represent an object."); auto hSubject = subject->Handle(); Handle<Function> hFunc; if (functionName != nullptr) // (if no name is given, assume the subject IS a function object, otherwise get the property as a function) { if (hSubject.IsEmpty() || !hSubject->IsObject()) throw exception("Call: The subject handle does not represent an object."); auto hProp = hSubject.As<Object>()->Get(NewUString(functionName)); if (hProp.IsEmpty() || !hProp->IsFunction()) throw exception("Call: The specified property does not represent a function."); hFunc = hProp.As<Function>(); } else if (hSubject.IsEmpty() || !hSubject->IsFunction()) throw exception("Call: The subject handle does not represent a function."); else hFunc = hSubject.As<Function>(); TryCatch __tryCatch; Handle<Value> result; if (argCount > 0) { Handle<Value>* _args = new Handle<Value>[argCount]; for (auto i = 0; i < argCount; i++) _args[i] = args[i]->Handle(); result = hFunc->Call(hThis.As<Object>(), argCount, _args); delete[] _args; } else result = hFunc->Call(hThis.As<Object>(), 0, nullptr); HandleProxy *returnVal; if (__tryCatch.HasCaught()) { returnVal = GetHandleProxy(GetErrorMessage(__tryCatch)); returnVal->_Type = JSV_ExecutionError; } else returnVal = result.IsEmpty() ? nullptr : GetHandleProxy(result); return returnVal; }
FunctionTemplateProxy::FunctionTemplateProxy(V8EngineProxy* engineProxy, uint16_t* className, ManagedJSFunctionCallback managedCallback) :ProxyBase(FunctionTemplateProxyClass), _EngineProxy(engineProxy), _EngineID(engineProxy->_EngineID) { // The function template will call the local "InvocationCallbackProxy" function, which then translates the call for the managed side. _FunctionTemplate = CopyablePersistent<FunctionTemplate>(NewFunctionTemplate(InvocationCallbackProxy, NewExternal(this))); _FunctionTemplate->SetClassName(NewUString(className)); _InstanceTemplate = new ObjectTemplateProxy(_EngineProxy, _FunctionTemplate->InstanceTemplate()); _PrototypeTemplate = new ObjectTemplateProxy(_EngineProxy, _FunctionTemplate->PrototypeTemplate()); SetManagedCallback(managedCallback); }
HandleProxy* V8EngineProxy::CreateString(const uint16_t* str) { return GetHandleProxy(NewUString(str)); }
void FunctionTemplateProxy::Set(const uint16_t *name, HandleProxy *value, v8::PropertyAttribute attributes) { _FunctionTemplate->Set(NewUString(name), value->Handle(), attributes); // TODO: Check how this affects objects created from templates! }