Skip to content

Commit

Permalink
A L8Runtime can now actually be deallocated. Verified use of GetCurre…
Browse files Browse the repository at this point in the history
…ntContext() in callbacks.

Because of the strong reference to the WrapperMap, and the WrapperMap containing a strong reference to the Runtime, plus the strong reference in the v8::Context, a Runtime would never deallocate. #1.
The use of GetCurrentContext() on the Isolate is correct, as in callbacks, the current context is always the context the callback came from. #5. (v8-users mailing list)
  • Loading branch information
joskuijpers committed Mar 18, 2014
1 parent 1523fa1 commit 2994495
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 22 deletions.
9 changes: 1 addition & 8 deletions L8Framework/Source/L8Runtime.mm
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ - (instancetype)initWithVirtualMachine:(L8VirtualMachine *)virtualMachine

// Create the context
Local<Context> context = Context::New(isolate);
context->SetEmbedderData(L8_RUNTIME_EMBEDDER_DATA_SELF, External::New(isolate,(void *)CFBridgingRetain(self)));
context->SetEmbedderData(L8_RUNTIME_EMBEDDER_DATA_SELF, External::New(isolate,(__bridge void *)self));
_v8context.Reset(isolate, context);

// Start the context scope
Expand All @@ -89,18 +89,11 @@ - (instancetype)initWithVirtualMachine:(L8VirtualMachine *)virtualMachine

- (void)dealloc
{
NSLog(@"Dealloc Context");

Isolate *isolate = _virtualMachine.V8Isolate;
HandleScope mainScope(isolate);
Local<Context> context = Local<Context>::New(isolate, _v8context);
Context::Scope contextScope(context);

Local<External> selfStored = context->GetEmbedderData(L8_RUNTIME_EMBEDDER_DATA_SELF).As<External>();
if(!selfStored.IsEmpty()) {
CFRelease(selfStored->Value());
}

_v8context.ClearAndLeak();
}

Expand Down
2 changes: 0 additions & 2 deletions L8Framework/Source/L8VirtualMachine.mm
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,12 @@ - (instancetype)init
valueOptions:NSPointerFunctionsStrongMemory | NSPointerFunctionsObjectPersonality
capacity:0];

NSLog(@"NEW isolate %p",_v8isolate);
}
return self;
}

- (void)dealloc
{
NSLog(@"Dealloc isolate %p",_v8isolate);
if(Isolate::GetCurrent() == _v8isolate)
_v8isolate->Exit();

Expand Down
1 change: 1 addition & 0 deletions L8Framework/Source/L8WrapperMap.mm
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,7 @@ SEL initializerSelectorForClass(Class cls)

@implementation L8WrapperMap {
std::map<std::string,Eternal<FunctionTemplate>> _classCache;
__weak L8Runtime *_runtime;
}

- (id)initWithRuntime:(L8Runtime *)runtime
Expand Down
23 changes: 11 additions & 12 deletions L8Framework/Source/ObjCCallback.mm
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,7 @@ inline void objCSetContextEmbedderData(const FunctionCallbackInfo<Value>& info)
{
Local<Context> context;

context = info.GetIsolate()->GetCurrentContext(); // TODO Verify
context = info.GetIsolate()->GetCurrentContext();

context->SetEmbedderData(L8_RUNTIME_EMBEDDER_DATA_CB_THIS, info.This());
context->SetEmbedderData(L8_RUNTIME_EMBEDDER_DATA_CB_CALLEE, info.Callee());
Expand All @@ -555,7 +555,7 @@ inline void objCClearContextEmbedderData(Isolate *isolate)
{
Local<Context> context;

context = isolate->GetCurrentContext(); // TODO Verify
context = isolate->GetCurrentContext();

context->SetEmbedderData(L8_RUNTIME_EMBEDDER_DATA_CB_THIS, Null(isolate));
context->SetEmbedderData(L8_RUNTIME_EMBEDDER_DATA_CB_CALLEE, Null(isolate));
Expand Down Expand Up @@ -609,7 +609,7 @@ void ObjCConstructor(const FunctionCallbackInfo<Value>& info)
// Set target
invocation.target = object;

context = [L8Runtime runtimeWithV8Context:isolate->GetCurrentContext()]; // TODO verify
context = [L8Runtime runtimeWithV8Context:isolate->GetCurrentContext()];

objCSetInvocationArguments(isolate, context, invocation, info, 2);
objCSetContextEmbedderData(info);
Expand Down Expand Up @@ -686,7 +686,7 @@ void ObjCMethodCall(const FunctionCallbackInfo<Value>& info)
invocation.selector = selector;
invocation.target = object;

context = [L8Runtime runtimeWithV8Context:isolate->GetCurrentContext()]; // TODO verify
context = [L8Runtime runtimeWithV8Context:isolate->GetCurrentContext()];

// Set the arguments
objCSetInvocationArguments(isolate, context, invocation, info, 2);
Expand Down Expand Up @@ -719,7 +719,7 @@ void ObjCBlockCall(const FunctionCallbackInfo<Value>& info)
invocation = [NSInvocation invocationWithMethodSignature:methodSignature];
invocation.target = block;

context = [L8Runtime runtimeWithV8Context:isolate->GetCurrentContext()]; // TODO verify
context = [L8Runtime runtimeWithV8Context:isolate->GetCurrentContext()];

// Set arguments (+1)
objCSetInvocationArguments(isolate, context, invocation, info, 1);
Expand All @@ -745,8 +745,7 @@ void ObjCNamedPropertySetter(Local<String> property, Local<Value> value, const P
L8Runtime *runtime;

object = objectFromWrapper(info.This()->GetInternalField(0));

runtime = [L8Runtime runtimeWithV8Context:info.GetIsolate()->GetCurrentContext()]; // TODO verify
runtime = [L8Runtime runtimeWithV8Context:info.GetIsolate()->GetCurrentContext()];

setValue = [L8Value valueWithV8Value:value inContext:runtime];

Expand All @@ -763,7 +762,7 @@ void ObjCNamedPropertyGetter(Local<String> property, const PropertyCallbackInfo<
object = objectFromWrapper(info.This()->GetInternalField(0));
value = [object objectForKeyedSubscript:[NSString stringWithV8String:property]];

runtime = [L8Runtime runtimeWithV8Context:info.GetIsolate()->GetCurrentContext()]; // TODO verify
runtime = [L8Runtime runtimeWithV8Context:info.GetIsolate()->GetCurrentContext()];

if(value)
info.GetReturnValue().Set(objectToValue(runtime, value));
Expand All @@ -777,7 +776,7 @@ void ObjCIndexedPropertySetter(uint32_t index, Local<Value> value, const Propert

object = objectFromWrapper(info.This()->GetInternalField(0));

runtime = [L8Runtime runtimeWithV8Context:info.GetIsolate()->GetCurrentContext()]; // TODO verify
runtime = [L8Runtime runtimeWithV8Context:info.GetIsolate()->GetCurrentContext()];

setValue = [L8Value valueWithV8Value:value inContext:runtime];
[object setObject:setValue atIndexedSubscript:index];
Expand All @@ -793,7 +792,7 @@ void ObjCIndexedPropertyGetter(uint32_t index, const PropertyCallbackInfo<Value>
object = objectFromWrapper(info.This()->GetInternalField(0));
value = [object objectAtIndexedSubscript:index];

runtime = [L8Runtime runtimeWithV8Context:info.GetIsolate()->GetCurrentContext()]; // TODO verify
runtime = [L8Runtime runtimeWithV8Context:info.GetIsolate()->GetCurrentContext()];

if(value)
info.GetReturnValue().Set(objectToValue(runtime, value));
Expand All @@ -815,7 +814,7 @@ void ObjCAccessorSetter(Local<String> property, Local<Value> value, const Proper
isolate = info.GetIsolate();
object = objectFromWrapper(info.This()->GetInternalField(0));
extraData = info.Data().As<Array>();
context = [L8Runtime runtimeWithV8Context:isolate->GetCurrentContext()]; // TODO Verify
context = [L8Runtime runtimeWithV8Context:isolate->GetCurrentContext()];

// 0 = name, 1 = value type, 2 = getter SEL, 3 = getter types, 4 = setter SEL, 5 = setter types
selector = selectorFromV8Value(extraData->Get(4));
Expand Down Expand Up @@ -854,7 +853,7 @@ void ObjCAccessorGetter(Local<String> property, const PropertyCallbackInfo<Value
isolate = info.GetIsolate();
object = objectFromWrapper(info.This()->GetInternalField(0));
extraData = info.Data().As<Array>();
context = [L8Runtime runtimeWithV8Context:isolate->GetCurrentContext()]; // TODO Verify
context = [L8Runtime runtimeWithV8Context:isolate->GetCurrentContext()];

// 0 = name, 1 = value type, 2 = getter SEL, 3 = getter types, 4 = setter SEL, 5 = setter types
returnType = createStringFromV8Value(extraData->Get(1));
Expand Down

0 comments on commit 2994495

Please sign in to comment.