From ae33f6a9ab8f91b494a676e2c8413007e6853171 Mon Sep 17 00:00:00 2001 From: Stefan Siegl Date: Wed, 26 Aug 2015 09:37:32 +0200 Subject: [PATCH] Re-use v8js_ctx context for V8Function calls, closes #158 --- v8js_v8.h | 7 ++++-- v8js_v8object_class.cc | 53 ++++++------------------------------------ 2 files changed, 12 insertions(+), 48 deletions(-) diff --git a/v8js_v8.h b/v8js_v8.h index 1e91970..8ad4d47 100644 --- a/v8js_v8.h +++ b/v8js_v8.h @@ -50,10 +50,10 @@ void v8js_terminate_execution(v8js_ctx *c TSRMLS_DC); /* Fetch V8 object properties */ int v8js_get_properties_hash(v8::Handle jsValue, HashTable *retval, int flags, v8::Isolate *isolate TSRMLS_DC); -#define V8JS_CTX_PROLOGUE(ctx) \ +#define V8JS_CTX_PROLOGUE_EX(ctx, ret) \ if (!V8JSG(v8_initialized)) { \ zend_error(E_ERROR, "V8 not initialized"); \ - return; \ + return ret; \ } \ \ v8::Isolate *isolate = (ctx)->isolate; \ @@ -63,6 +63,9 @@ int v8js_get_properties_hash(v8::Handle jsValue, HashTable *retval, i v8::Local v8_context = v8::Local::New(isolate, (ctx)->context); \ v8::Context::Scope context_scope(v8_context); +#define V8JS_CTX_PROLOGUE(ctx) \ + V8JS_CTX_PROLOGUE_EX(ctx,) + #define V8JS_BEGIN_CTX(ctx, object) \ v8js_ctx *(ctx); \ (ctx) = (v8js_ctx *) zend_object_store_get_object(object TSRMLS_CC); \ diff --git a/v8js_v8object_class.cc b/v8js_v8object_class.cc index e68f874..debabff 100644 --- a/v8js_v8object_class.cc +++ b/v8js_v8object_class.cc @@ -60,13 +60,7 @@ static int v8js_v8object_has_property(zval *object, zval *member, int has_set_ex return retval; } - v8::Isolate *isolate = obj->ctx->isolate; - v8::Locker locker(isolate); - v8::Isolate::Scope isolate_scope(isolate); - v8::HandleScope local_scope(isolate); - v8::Local temp_context = v8::Context::New(isolate); - v8::Context::Scope temp_scope(temp_context); - + V8JS_CTX_PROLOGUE_EX(obj->ctx, retval); v8::Local v8obj = v8::Local::New(isolate, obj->v8obj); if (Z_TYPE_P(member) == IS_STRING && v8obj->IsObject() && !v8obj->IsFunction()) @@ -126,13 +120,7 @@ static zval *v8js_v8object_read_property(zval *object, zval *member, int type ZE return retval; } - v8::Isolate *isolate = obj->ctx->isolate; - v8::Locker locker(isolate); - v8::Isolate::Scope isolate_scope(isolate); - v8::HandleScope local_scope(isolate); - v8::Local temp_context = v8::Context::New(isolate); - v8::Context::Scope temp_scope(temp_context); - + V8JS_CTX_PROLOGUE_EX(obj->ctx, retval); v8::Local v8obj = v8::Local::New(isolate, obj->v8obj); if (Z_TYPE_P(member) == IS_STRING && v8obj->IsObject() && !v8obj->IsFunction()) @@ -175,13 +163,7 @@ static void v8js_v8object_write_property(zval *object, zval *member, zval *value return; } - v8::Isolate *isolate = obj->ctx->isolate; - v8::Locker locker(isolate); - v8::Isolate::Scope isolate_scope(isolate); - v8::HandleScope local_scope(isolate); - v8::Local temp_context = v8::Context::New(isolate); - v8::Context::Scope temp_scope(temp_context); - + V8JS_CTX_PROLOGUE(obj->ctx); v8::Local v8obj = v8::Local::New(isolate, obj->v8obj); if (v8obj->IsObject() && !v8obj->IsFunction()) { @@ -200,13 +182,7 @@ static void v8js_v8object_unset_property(zval *object, zval *member ZEND_HASH_KE return; } - v8::Isolate *isolate = obj->ctx->isolate; - v8::Locker locker(isolate); - v8::Isolate::Scope isolate_scope(isolate); - v8::HandleScope local_scope(isolate); - v8::Local temp_context = v8::Context::New(isolate); - v8::Context::Scope temp_scope(temp_context); - + V8JS_CTX_PROLOGUE(obj->ctx); v8::Local v8obj = v8::Local::New(isolate, obj->v8obj); if (v8obj->IsObject() && !v8obj->IsFunction()) { @@ -245,12 +221,7 @@ static HashTable *v8js_v8object_get_properties(zval *object TSRMLS_DC) /* {{{ */ return NULL; } - v8::Isolate *isolate = obj->ctx->isolate; - v8::Locker locker(isolate); - v8::Isolate::Scope isolate_scope(isolate); - v8::HandleScope local_scope(isolate); - v8::Local temp_context = v8::Context::New(isolate); - v8::Context::Scope temp_scope(temp_context); + V8JS_CTX_PROLOGUE_EX(obj->ctx, NULL); v8::Local v8obj = v8::Local::New(isolate, obj->v8obj); if (v8js_get_properties_hash(v8obj, obj->properties, obj->flags, isolate TSRMLS_CC) == SUCCESS) { @@ -279,12 +250,7 @@ static zend_function *v8js_v8object_get_method(zval **object_ptr, char *method, return NULL; } - v8::Isolate *isolate = obj->ctx->isolate; - v8::Locker locker(isolate); - v8::Isolate::Scope isolate_scope(isolate); - v8::HandleScope local_scope(isolate); - v8::Local temp_context = v8::Context::New(isolate); - v8::Context::Scope temp_scope(temp_context); + V8JS_CTX_PROLOGUE_EX(obj->ctx, NULL); v8::Local jsKey = V8JS_STRL(method, method_len); v8::Local v8obj = v8::Local::New(isolate, obj->v8obj); @@ -378,12 +344,7 @@ static int v8js_v8object_get_closure(zval *object, zend_class_entry **ce_ptr, ze return FAILURE; } - v8::Isolate *isolate = obj->ctx->isolate; - v8::Locker locker(isolate); - v8::Isolate::Scope isolate_scope(isolate); - v8::HandleScope local_scope(isolate); - v8::Local temp_context = v8::Context::New(isolate); - v8::Context::Scope temp_scope(temp_context); + V8JS_CTX_PROLOGUE_EX(obj->ctx, FAILURE); v8::Local v8obj = v8::Local::New(isolate, obj->v8obj); if (!v8obj->IsFunction()) {