HandleProxy* V8EngineProxy::SetGlobalObjectTemplate(ObjectTemplateProxy* proxy) { if (!_Context.IsEmpty()) _Context.Reset(); if (_GlobalObjectTemplateProxy != nullptr) delete _GlobalObjectTemplateProxy; _GlobalObjectTemplateProxy = proxy; auto context = v8::Context::New(_Isolate, nullptr, Local<ObjectTemplate>::New(_Isolate, _GlobalObjectTemplateProxy->_ObjectTemplate)); _Context = context; // ... the context auto creates the global object from the given template, BUT, we still need to update the internal fields with proper values expected // for callback into managed code ... auto globalObject = context->Global()->GetPrototype()->ToObject(); globalObject->SetAlignedPointerInInternalField(0, _GlobalObjectTemplateProxy); // (proxy object reference) globalObject->SetInternalField(1, External::New(_Isolate, (void*)-1)); // (manage object ID, which is only applicable when tracking many created objects [and not a single engine or global scope]) _GlobalObject = globalObject; // (keep a reference to the global object for faster reference) return GetHandleProxy(globalObject); // (the native side will own this, and is responsible to free it when done) }
HandleProxy* FunctionTemplateProxy::CreateInstance(int32_t managedObjectID, int32_t argCount, HandleProxy** args) { auto hArgs = new Handle<Value>[argCount]; for (auto i = 0; i < argCount; i++) hArgs[i] = args[i]->Handle(); auto obj = _FunctionTemplate->GetFunction()->NewInstance(argCount, hArgs); delete[] hArgs; // TODO: (does "disposed" still need to be called here for each item?) if (managedObjectID == -1) managedObjectID = _EngineProxy->GetNextNonTemplateObjectID(); auto proxyVal = _EngineProxy->GetHandleProxy(obj); proxyVal->_ObjectID = managedObjectID; //??auto count = obj->InternalFieldCount(); obj->SetAlignedPointerInInternalField(0, this); // (stored a reference to the proxy instance for the call-back functions) obj->SetInternalField(1, NewExternal(reinterpret_cast<void*>(managedObjectID))); // (stored a reference to the managed object for the call-back functions) obj->SetHiddenValue(NewString("ManagedObjectID"), NewInteger(managedObjectID)); // (won't be used on template created objects [fields are faster], but done anyhow for consistency) return proxyVal; }