On Wed, Jul 8, 2015 at 5:30 PM, Juggle <[email protected]> wrote:
> Hi.
>
> I'm trying to develop nodejs module on C++ using v8.
>
> Here is source code.
>
>     #include <node.h>
>
>
>     using namespace v8;
>
>
>     void TestContext1(const FunctionCallbackInfo<Value>& args) {
>
>
>         Isolate* isolate = Isolate::New();
>         {
>             Isolate::Scope isolate_scope(isolate);
>             V8::Initialize();
>             Locker lock(isolate);
>
>
>             HandleScope handle_scope(isolate);
>
>
>             Local<Context> context = Context::New(isolate);
>             Context::Scope context_scope(context);
>
>
>
>
>             Handle<String> strExpr = Handle<String>::Cast(args[0]);
>             Handle<Script> script;
>             Handle<Value> res;
>
>
>             TryCatch tryCatch;
>             script = Script::Compile(strExpr);
>             res = script->Run();
>
>
>             if (tryCatch.HasCaught()) {
>                 res = tryCatch.Message()->Get();
>             }
>
>
>             args.GetReturnValue().Set(res);
>         }
>         isolate->Dispose();
>
>
>     }
>
>
>
>
>     void TestContext2(const FunctionCallbackInfo<Value>& args) {
>
>
>         Isolate *isolate = args.GetIsolate();
>         Isolate::Scope isolate_scope(isolate);
>         HandleScope handle_scope(isolate);
>         Local<Context> context = Context::New(isolate);
>         Context::Scope context_scope(context);
>
>
>         Handle<String> strExpr = Handle<String>::Cast(args[0]);
>         Handle<Script> script;
>         Handle<Value> res;
>
>
>         TryCatch tryCatch;
>         script = Script::Compile(strExpr);
>         res = script->Run();
>
>
>         if (tryCatch.HasCaught()) {
>             res = tryCatch.Message()->Get();
>         }
>
>
>         args.GetReturnValue().Set(res);
>     }
>
>
>     void Init(Handle<Object> exports, Handle<Object> module) {
>         NODE_SET_METHOD(exports, "TestContext1", TestContext1);
>         NODE_SET_METHOD(exports, "TestContext2", TestContext2);
>     }
>
>
>     NODE_MODULE(addon, Init)
>
>
> And then testing it with js code:
>
>     var addon = require('addon.node')
>     var i = 0;
>     setInterval(function () {
>         msg = addon.TestContext1("process.exit()"); // or msg =
> addon.TestContext2("process.exit()");
>         console.log(msg, i++, process.memoryUsage().heapUsed/1024/1024);
>     }, 2);
>
>
> Every function have different problems:
>
> **TestContext1** works fine but accessing global context and shutting down
> process.
>
> **TestContext2** does not have access to global context but gives memory
> leaks.
>
> All what I need - execute js script in isolated context without memory
> leaks.
>
> node vm doesn't work because vm.runInNewContext() have same memory leaks.
>
> Does anybody have some ideas?

Sample 1 is unsafe because you're passing a value from one isolate to
another.  Don't do that, it won't work.

Sample 2 may benefit from calling
isolate->ContextDisposedNotification() and, possibly,
isolate->LowMemoryNotification(), but it won't help when complex
values like arrays and objects from the temporary context escape to
the main context.  JS objects retain references to things like their
constructors and prototypes, which in turn retain references to their
constructors and prototypes, etc.  You quickly end up retaining most
of the context.

-- 
-- 
v8-users mailing list
[email protected]
http://groups.google.com/group/v8-users
--- 
You received this message because you are subscribed to the Google Groups 
"v8-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to