mirror of
https://github.com/ONLYOFFICE/core.git
synced 2026-02-10 18:05:41 +08:00
[V8] Add FreeEmbedObject function
This commit is contained in:
@ -214,8 +214,9 @@ namespace NSJSBase
|
||||
m_internal->m_contextPersistent.Reset(isolate, v8::Context::New(isolate));
|
||||
// create temporary local handle to context
|
||||
m_internal->m_context = v8::Local<v8::Context>::New(isolate, m_internal->m_contextPersistent);
|
||||
// insert CreateEmbedObject() function to global object of this context
|
||||
// insert embed functions to global object of this context
|
||||
m_internal->InsertToGlobal("CreateEmbedObject", CreateEmbedNativeObject);
|
||||
m_internal->InsertToGlobal("FreeEmbedObject", FreeNativeObject);
|
||||
// clear temporary local handle
|
||||
m_internal->m_context.Clear();
|
||||
}
|
||||
@ -689,4 +690,22 @@ namespace NSJSBase
|
||||
NSJSBase::CJSEmbedObjectPrivate::CreateWeaker(obj);
|
||||
args.GetReturnValue().Set(obj);
|
||||
}
|
||||
|
||||
void FreeNativeObject(const v8::FunctionCallbackInfo<v8::Value>& args)
|
||||
{
|
||||
v8::Isolate* isolate = args.GetIsolate();
|
||||
v8::HandleScope scope(isolate);
|
||||
|
||||
if (args.Length() != 1)
|
||||
{
|
||||
args.GetReturnValue().Set(v8::Undefined(isolate));
|
||||
return;
|
||||
}
|
||||
|
||||
v8::Local<v8::Object> obj = args[0].As<v8::Object>();
|
||||
v8::Local<v8::External> field = v8::Local<v8::External>::Cast(obj->GetInternalField(0));
|
||||
CJSEmbedObject* native = static_cast<CJSEmbedObject*>(field->Value());
|
||||
delete native;
|
||||
// weak persistent handle will be cleared and removed in CJSEmbedObjectPrivate destructor
|
||||
}
|
||||
}
|
||||
|
||||
@ -883,6 +883,7 @@ namespace NSJSBase
|
||||
|
||||
// embed
|
||||
void CreateEmbedNativeObject(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
void FreeNativeObject(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
|
||||
class CJSEmbedObjectAdapterV8Template : public CJSEmbedObjectAdapterBase
|
||||
{
|
||||
|
||||
@ -5,10 +5,10 @@ int main()
|
||||
// TODO: test
|
||||
|
||||
// testMultipleContexts();
|
||||
// testEmbedExternal();
|
||||
testEmbedExternal();
|
||||
// testEmbedInternal();
|
||||
// testHashEmbed();
|
||||
testEmbedMixed();
|
||||
// testEmbedMixed();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -98,16 +98,16 @@ void testEmbedExternal()
|
||||
CJSContextScope scope(context);
|
||||
CJSContext::Embed<CTestEmbed>();
|
||||
|
||||
JSSmart<CJSValue> res1 = context->runScript("(function() { let value = CreateEmbedObject('CTestEmbed'); let ret = value.FunctionSum(10, 5); return ret; })();");
|
||||
JSSmart<CJSValue> res1 = context->runScript("(function() { let value = CreateEmbedObject('CTestEmbed'); let ret = value.FunctionSum(10, 5); FreeEmbedObject(value); return ret; })();");
|
||||
std::cout << "FunctionSum(10, 5) = " << res1->toInt32() << std::endl;
|
||||
|
||||
JSSmart<CJSValue> res2 = context->runScript("(function() { let value = CreateEmbedObject('CTestEmbed'); let ret = value.FunctionSquare(4); return ret; })();");
|
||||
JSSmart<CJSValue> res2 = context->runScript("(function() { let value = CreateEmbedObject('CTestEmbed'); let ret = value.FunctionSquare(4); FreeEmbedObject(value); return ret; })();");
|
||||
std::cout << "FunctionSquare(4) = " << res2->toInt32() << std::endl;
|
||||
|
||||
JSSmart<CJSValue> res3 = context->runScript("(function() { let value = CreateEmbedObject('CTestEmbed'); let ret = value.FunctionDel(30, 3); return ret; })();");
|
||||
JSSmart<CJSValue> res3 = context->runScript("(function() { let value = CreateEmbedObject('CTestEmbed'); let ret = value.FunctionDel(30, 3); FreeEmbedObject(value); return ret; })();");
|
||||
std::cout << "FunctionDel(30, 3) = " << res3->toInt32() << std::endl;
|
||||
|
||||
JSSmart<CJSValue> res4 = context->runScript("(function() { let value = CreateEmbedObject('CTestEmbed'); let ret = value.FunctionGet(); return ret; })();");
|
||||
JSSmart<CJSValue> res4 = context->runScript("(function() { let value = CreateEmbedObject('CTestEmbed'); let ret = value.FunctionGet(); FreeEmbedObject(value); return ret; })();");
|
||||
std::cout << "FunctionGet() = " << res4->toInt32() << std::endl;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user