diff options
author | Stefan Radomski <github@mintwerk.de> | 2017-05-10 20:48:14 (GMT) |
---|---|---|
committer | Stefan Radomski <github@mintwerk.de> | 2017-05-10 20:48:14 (GMT) |
commit | a33a96fd7aee6d53f663102c56236e91d77f53a7 (patch) | |
tree | 3a27a07f8f4eeddd233b2bb8ed9a5c43c36077c6 /src/uscxml/plugins/datamodel/ecmascript | |
parent | 5083ea697c263a507341c98c5dadbb23953bd4fb (diff) | |
download | uscxml-a33a96fd7aee6d53f663102c56236e91d77f53a7.zip uscxml-a33a96fd7aee6d53f663102c56236e91d77f53a7.tar.gz uscxml-a33a96fd7aee6d53f663102c56236e91d77f53a7.tar.bz2 |
Fixed issue 114
Diffstat (limited to 'src/uscxml/plugins/datamodel/ecmascript')
4 files changed, 40 insertions, 33 deletions
diff --git a/src/uscxml/plugins/datamodel/ecmascript/JavaScriptCore/JSCDataModel.cpp b/src/uscxml/plugins/datamodel/ecmascript/JavaScriptCore/JSCDataModel.cpp index 1358f8b..124da6e 100644 --- a/src/uscxml/plugins/datamodel/ecmascript/JavaScriptCore/JSCDataModel.cpp +++ b/src/uscxml/plugins/datamodel/ecmascript/JavaScriptCore/JSCDataModel.cpp @@ -246,9 +246,13 @@ JSValueRef JSCNodeListGetPropertyCallback(JSContextRef context, JSObjectRef obje std::shared_ptr<DataModelImpl> JSCDataModel::create(DataModelCallbacks* callbacks) { std::shared_ptr<JSCDataModel> dm(new JSCDataModel()); - - dm->_ctx = JSGlobalContextCreate(NULL); dm->_callbacks = callbacks; + dm->setup(); + return dm; +} + +void JSCDataModel::setup() { + _ctx = JSGlobalContextCreate(NULL); #ifndef NO_XERCESC JSObjectRef exports; @@ -260,50 +264,48 @@ std::shared_ptr<DataModelImpl> JSCDataModel::create(DataModelCallbacks* callback // not thread safe! { std::lock_guard<std::mutex> lock(_initMutex); - JSCDOM_initialize(dm->_ctx, &exports); + JSCDOM_initialize(_ctx, &exports); } #endif // introduce global functions as objects for private data JSClassRef jsInClassRef = JSClassCreate(&jsInClassDef); - JSObjectRef jsIn = JSObjectMake(dm->_ctx, jsInClassRef, dm.get()); + JSObjectRef jsIn = JSObjectMake(_ctx, jsInClassRef, this); JSStringRef inName = JSStringCreateWithUTF8CString("In"); - JSObjectSetProperty(dm->_ctx, JSContextGetGlobalObject(dm->_ctx), inName, jsIn, kJSPropertyAttributeNone, NULL); + JSObjectSetProperty(_ctx, JSContextGetGlobalObject(_ctx), inName, jsIn, kJSPropertyAttributeNone, NULL); JSStringRelease(inName); JSClassRef jsPrintClassRef = JSClassCreate(&jsPrintClassDef); - JSObjectRef jsPrint = JSObjectMake(dm->_ctx, jsPrintClassRef, dm.get()); + JSObjectRef jsPrint = JSObjectMake(_ctx, jsPrintClassRef, this); JSStringRef printName = JSStringCreateWithUTF8CString("print"); - JSObjectSetProperty(dm->_ctx, JSContextGetGlobalObject(dm->_ctx), printName, jsPrint, kJSPropertyAttributeNone, NULL); + JSObjectSetProperty(_ctx, JSContextGetGlobalObject(_ctx), printName, jsPrint, kJSPropertyAttributeNone, NULL); JSStringRelease(printName); JSClassRef jsInvokerClassRef = JSClassCreate(&jsInvokersClassDef); - JSObjectRef jsInvoker = JSObjectMake(dm->_ctx, jsInvokerClassRef, dm.get()); + JSObjectRef jsInvoker = JSObjectMake(_ctx, jsInvokerClassRef, this); JSStringRef invokerName = JSStringCreateWithUTF8CString("_invokers"); - JSObjectSetProperty(dm->_ctx, JSContextGetGlobalObject(dm->_ctx), invokerName, jsInvoker, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete, NULL); + JSObjectSetProperty(_ctx, JSContextGetGlobalObject(_ctx), invokerName, jsInvoker, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete, NULL); JSStringRelease(invokerName); JSClassRef jsIOProcClassRef = JSClassCreate(&jsIOProcessorsClassDef); - JSObjectRef jsIOProc = JSObjectMake(dm->_ctx, jsIOProcClassRef, dm.get()); + JSObjectRef jsIOProc = JSObjectMake(_ctx, jsIOProcClassRef, this); JSStringRef ioProcName = JSStringCreateWithUTF8CString("_ioprocessors"); - JSObjectSetProperty(dm->_ctx, JSContextGetGlobalObject(dm->_ctx), ioProcName, jsIOProc, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete, NULL); + JSObjectSetProperty(_ctx, JSContextGetGlobalObject(_ctx), ioProcName, jsIOProc, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete, NULL); JSStringRelease(ioProcName); JSStringRef nameName = JSStringCreateWithUTF8CString("_name"); - JSStringRef name = JSStringCreateWithUTF8CString(dm->_callbacks->getName().c_str()); - JSObjectSetProperty(dm->_ctx, JSContextGetGlobalObject(dm->_ctx), nameName, JSValueMakeString(dm->_ctx, name), kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete, NULL); + JSStringRef name = JSStringCreateWithUTF8CString(_callbacks->getName().c_str()); + JSObjectSetProperty(_ctx, JSContextGetGlobalObject(_ctx), nameName, JSValueMakeString(_ctx, name), kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete, NULL); JSStringRelease(nameName); JSStringRelease(name); JSStringRef sessionIdName = JSStringCreateWithUTF8CString("_sessionid"); - JSStringRef sessionId = JSStringCreateWithUTF8CString(dm->_callbacks->getSessionId().c_str()); - JSObjectSetProperty(dm->_ctx, JSContextGetGlobalObject(dm->_ctx), sessionIdName, JSValueMakeString(dm->_ctx, sessionId), kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete, NULL); + JSStringRef sessionId = JSStringCreateWithUTF8CString(_callbacks->getSessionId().c_str()); + JSObjectSetProperty(_ctx, JSContextGetGlobalObject(_ctx), sessionIdName, JSValueMakeString(_ctx, sessionId), kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete, NULL); JSStringRelease(sessionIdName); JSStringRelease(sessionId); - dm->evalAsValue("_x = {};"); - - return dm; + evalAsValue("_x = {};"); } void JSCDataModel::setEvent(const Event& event) { diff --git a/src/uscxml/plugins/datamodel/ecmascript/JavaScriptCore/JSCDataModel.h b/src/uscxml/plugins/datamodel/ecmascript/JavaScriptCore/JSCDataModel.h index 5a0e830..977385c 100644 --- a/src/uscxml/plugins/datamodel/ecmascript/JavaScriptCore/JSCDataModel.h +++ b/src/uscxml/plugins/datamodel/ecmascript/JavaScriptCore/JSCDataModel.h @@ -89,6 +89,7 @@ public: const std::map<std::string, std::string>& attr = std::map<std::string, std::string>()); protected: + virtual void setup(); static JSClassDefinition jsInClassDef; static JSValueRef jsIn(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception); diff --git a/src/uscxml/plugins/datamodel/ecmascript/v8/V8DataModel.cpp b/src/uscxml/plugins/datamodel/ecmascript/v8/V8DataModel.cpp index e3820c2..a7942a1 100644 --- a/src/uscxml/plugins/datamodel/ecmascript/v8/V8DataModel.cpp +++ b/src/uscxml/plugins/datamodel/ecmascript/v8/V8DataModel.cpp @@ -176,35 +176,39 @@ void V8NodeListIndexedPropertyHandler(uint32_t index, const v8::PropertyCallback std::shared_ptr<DataModelImpl> V8DataModel::create(DataModelCallbacks* callbacks) { std::shared_ptr<V8DataModel> dm(new V8DataModel()); dm->_callbacks = callbacks; + dm->setup(); + return dm; +} +void V8DataModel::setup() { // TODO: we cannot use one isolate per thread as swig's type will be unknown :( // We could register them by hand and avoid the _export_ globals in swig? - if (dm->_isolate == NULL) { - dm->_isolate = v8::Isolate::New(); + if (_isolate == NULL) { + _isolate = v8::Isolate::New(); } - v8::Locker locker(dm->_isolate); - v8::Isolate::Scope isoScope(dm->_isolate); + v8::Locker locker(_isolate); + v8::Isolate::Scope isoScope(_isolate); // Create a handle scope to hold the temporary references. - v8::HandleScope scope(dm->_isolate); + v8::HandleScope scope(_isolate); // Create a template for the global object where we set the built-in global functions. v8::Local<v8::ObjectTemplate> global = v8::ObjectTemplate::New(); // some free functions global->Set(v8::String::NewSymbol("print"), - v8::FunctionTemplate::New(dm->_isolate, jsPrint,v8::External::New(reinterpret_cast<void*>(dm.get())))); + v8::FunctionTemplate::New(_isolate, jsPrint,v8::External::New(reinterpret_cast<void*>(this)))); global->Set(v8::String::NewSymbol("In"), - v8::FunctionTemplate::New(dm->_isolate, jsIn, v8::External::New(reinterpret_cast<void*>(dm.get())))); + v8::FunctionTemplate::New(_isolate, jsIn, v8::External::New(reinterpret_cast<void*>(this)))); - v8::Local<v8::Context> context = v8::Context::New(dm->_isolate, NULL, global); + v8::Local<v8::Context> context = v8::Context::New(_isolate, NULL, global); - dm->_context.Reset(dm->_isolate, context); + _context.Reset(_isolate, context); // Enter the new context so all the following operations take place within it. v8::Context::Scope contextScope(context); - assert(dm->_isolate->GetCurrentContext() == context); + assert(_isolate->GetCurrentContext() == context); #ifndef NO_XERCESC @@ -236,11 +240,11 @@ std::shared_ptr<DataModelImpl> V8DataModel::create(DataModelCallbacks* callbacks context->Global()->SetAccessor(v8::String::NewSymbol("_ioprocessors"), V8DataModel::getIOProcessors, V8DataModel::setWithException, - v8::External::New(reinterpret_cast<void*>(dm.get()))); + v8::External::New(reinterpret_cast<void*>(this))); context->Global()->SetAccessor(v8::String::NewSymbol("_invokers"), V8DataModel::getInvokers, V8DataModel::setWithException, - v8::External::New(reinterpret_cast<void*>(dm.get()))); + v8::External::New(reinterpret_cast<void*>(this))); // v8::Persistent<v8::Value, v8::CopyablePersistentTraits<v8::Value> > persistent(_isolate, context); @@ -252,7 +256,7 @@ std::shared_ptr<DataModelImpl> V8DataModel::create(DataModelCallbacks* callbacks V8Document::V8DocumentPrivate* privData = new V8Document::V8DocumentPrivate(); privData->nativeObj = new Document<std::string>(interpreter->getDocument()); - privData->dom = dm->_dom; + privData->dom = _dom; docObj->SetInternalField(0, V8DOM::toExternal(privData)); context->Global()->Set(v8::String::New("document"), docObj); @@ -273,10 +277,9 @@ std::shared_ptr<DataModelImpl> V8DataModel::create(DataModelCallbacks* callbacks // instantiate objects - we have to have a context for that! - dm->eval(Element<std::string>(), "_x = {};"); + eval(Element<std::string>(), "_x = {};"); #endif - return dm; } void V8DataModel::getAttribute(v8::Local<v8::String> property, const v8::PropertyCallbackInfo<v8::Value>& info) { diff --git a/src/uscxml/plugins/datamodel/ecmascript/v8/V8DataModel.h b/src/uscxml/plugins/datamodel/ecmascript/v8/V8DataModel.h index 00a871b..e286c56 100644 --- a/src/uscxml/plugins/datamodel/ecmascript/v8/V8DataModel.h +++ b/src/uscxml/plugins/datamodel/ecmascript/v8/V8DataModel.h @@ -82,6 +82,7 @@ public: const std::map<std::string, std::string>& attr = std::map<std::string, std::string>()); protected: + virtual void setup(); static void jsExtension(const v8::FunctionCallbackInfo<v8::Value>& info); static void jsIn(const v8::FunctionCallbackInfo<v8::Value>& info); |