From 4b9c6224ad9f798f3a14bd1a9fb4796f004c15fb Mon Sep 17 00:00:00 2001 From: Sam Saffron Date: Mon, 9 May 2016 15:24:43 +1000 Subject: [PATCH] attempting to get function attaching working --- .../mini_racer_extension.cc | 21 ++++++++++++------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/ext/mini_racer_extension/mini_racer_extension.cc b/ext/mini_racer_extension/mini_racer_extension.cc index 33fab92..b730a9f 100644 --- a/ext/mini_racer_extension/mini_racer_extension.cc +++ b/ext/mini_racer_extension/mini_racer_extension.cc @@ -11,6 +11,7 @@ using namespace v8; typedef struct { Isolate* isolate; Persistent* context; + Persistent* globals; } ContextInfo; typedef struct { @@ -187,11 +188,8 @@ static VALUE rb_context_notify(VALUE self, VALUE str) { Local v8_str = String::NewFromUtf8(context_info->isolate, RSTRING_PTR(str), NewStringType::kNormal, (int)RSTRING_LEN(str)).ToLocalChecked(); - Local global = ObjectTemplate::New(context_info->isolate); - global->Set(v8_str, FunctionTemplate::New(context_info->isolate, RubyCallback)); - - Context::New(context_info->isolate, NULL, global); + context_info->globals->Set(v8_str, FunctionTemplate::New(context_info->isolate, RubyCallback)); return Qnil; } @@ -226,14 +224,21 @@ VALUE allocate(VALUE klass) { create_params.array_buffer_allocator = allocator; context_info->isolate = Isolate::New(create_params); - Locker lock(context_info->isolate); Isolate::Scope isolate_scope(context_info->isolate); HandleScope handle_scope(context_info->isolate); - Local context = Context::New(context_info->isolate); - context_info->context = new Persistent(); - context_info->context->Reset(context_info->isolate, context); + Local globals = ObjectTemplate::New(context_info->isolate); + Persistent persistent_globals; + Persistent persistent_context; + + Local context = Context::New(context_info->isolate, NULL, globals); + + persistent_globals.Reset(context_info->isolate, globals); + persistent_context.Reset(context_info->isolate, context); + + context_info->globals = &persistent_globals; + context_info->context = &persistent_context; return Data_Wrap_Struct(klass, NULL, deallocate, (void*)context_info); }