@@ -116,6 +116,15 @@ namespace Noesis { namespace Javascript {
116
116
}
117
117
#pragma managed(pop)
118
118
119
+ v8::Local<v8::String> ToV8String (Isolate* isolate, System::String^ value) {
120
+ if (value == nullptr )
121
+ throw gcnew System::ArgumentNullException (" value" );
122
+ pin_ptr<const wchar_t > namePtr = PtrToStringChars (value);
123
+ wchar_t * name = (wchar_t *)namePtr;
124
+
125
+ return String::NewFromTwoByte (isolate, (uint16_t *)name, v8::NewStringType::kNormal ).ToLocalChecked ();
126
+ }
127
+
119
128
static JavascriptContext::JavascriptContext ()
120
129
{
121
130
System::Threading::Mutex mutex (true , " FA12B681-E968-4D3A-833D-43B25865BEF1" );
@@ -173,6 +182,7 @@ JavascriptContext::JavascriptContext()
173
182
isolate->SetFatalErrorHandler (FatalErrorCallback);
174
183
175
184
mExternals = gcnew System::Collections::Generic::Dictionary<System::Object ^, WrappedJavascriptExternal>();
185
+ mTypeToConstructorMapping = gcnew System::Collections::Generic::Dictionary<System::Type ^, System::IntPtr>();
176
186
mFunctions = gcnew System::Collections::Generic::List<System::WeakReference ^>();
177
187
HandleScope scope (isolate);
178
188
mContext = new Persistent<Context>(isolate, Context::New (isolate));
@@ -194,8 +204,12 @@ JavascriptContext::~JavascriptContext()
194
204
delete function;
195
205
}
196
206
mFunctions ->Clear ();
207
+ for each (System::IntPtr p in mTypeToConstructorMapping ->Values ) {
208
+ delete (void *)p;
209
+ }
197
210
delete mContext ;
198
211
delete mExternals ;
212
+ delete mTypeToConstructorMapping ;
199
213
delete mFunctions ;
200
214
}
201
215
if (isolate != NULL )
@@ -255,10 +269,6 @@ JavascriptContext::SetParameter(System::String^ iName, System::Object^ iObject)
255
269
void
256
270
JavascriptContext::SetParameter (System::String^ iName, System::Object^ iObject, SetParameterOptions options)
257
271
{
258
- if (iName == nullptr )
259
- throw gcnew System::ArgumentNullException (" iName" );
260
- pin_ptr<const wchar_t > namePtr = PtrToStringChars (iName);
261
- wchar_t * name = (wchar_t *) namePtr;
262
272
JavascriptScope scope (this );
263
273
v8::Isolate *isolate = JavascriptContext::GetCurrentIsolate ();
264
274
HandleScope handleScope (isolate);
@@ -276,12 +286,27 @@ JavascriptContext::SetParameter(System::String^ iName, System::Object^ iObject,
276
286
}
277
287
}
278
288
279
- v8::Local<v8::String> key = String::NewFromTwoByte (isolate, ( uint16_t *)name, v8::NewStringType:: kNormal ). ToLocalChecked ( );
289
+ v8::Local<v8::String> key = ToV8String (isolate, iName );
280
290
Local<Context>::New (isolate, *mContext )->Global ()->Set (isolate->GetCurrentContext (), key, value).ToChecked ();
281
291
}
282
292
283
293
// //////////////////////////////////////////////////////////////////////////////////////////////////
284
294
295
+
296
+ void JavascriptContext::SetConstructor (System::String^ name, System::Type^ associatedType, System::Delegate^ constructor)
297
+ {
298
+ JavascriptScope scope (this );
299
+ v8::Isolate *isolate = JavascriptContext::GetCurrentIsolate ();
300
+ HandleScope handleScope (isolate);
301
+
302
+ Handle <FunctionTemplate> functionTemplate = JavascriptInterop::GetFunctionTemplateFromSystemDelegate (constructor);
303
+ JavascriptInterop::InitObjectWrapperTemplate (functionTemplate->InstanceTemplate ());
304
+ mTypeToConstructorMapping [associatedType] = System::IntPtr (new Persistent<FunctionTemplate>(isolate, functionTemplate));
305
+ Local<Context>::New (isolate, *mContext )->Global ()->Set (isolate->GetCurrentContext (), ToV8String (isolate, name), functionTemplate->GetFunction ());
306
+ }
307
+
308
+ // //////////////////////////////////////////////////////////////////////////////////////////////////
309
+
285
310
System::Object^
286
311
JavascriptContext::GetParameter (System::String^ iName)
287
312
{
@@ -507,12 +532,18 @@ JavascriptContext::WrapObject(System::Object^ iObject)
507
532
508
533
// //////////////////////////////////////////////////////////////////////////////////////////////////
509
534
510
- Handle <ObjectTemplate >
511
- JavascriptContext::GetObjectWrapperTemplate ( )
535
+ Handle <FunctionTemplate >
536
+ JavascriptContext::GetObjectWrapperConstructorTemplate (System::Type ^type )
512
537
{
513
- if (objectWrapperTemplate == NULL )
514
- objectWrapperTemplate = new Persistent<ObjectTemplate>(isolate, JavascriptInterop::NewObjectWrapperTemplate ());
515
- return Local<ObjectTemplate>::New (isolate, *objectWrapperTemplate);
538
+ System::IntPtr ptrToConstructor;
539
+ if (!mTypeToConstructorMapping ->TryGetValue (type, ptrToConstructor)) {
540
+ Local<FunctionTemplate> constructor = FunctionTemplate::New (GetCurrentIsolate ());
541
+ JavascriptInterop::InitObjectWrapperTemplate (constructor->InstanceTemplate ());
542
+ mTypeToConstructorMapping [type] = System::IntPtr (new Persistent<FunctionTemplate>(isolate, constructor));
543
+ return constructor;
544
+ }
545
+ Persistent<FunctionTemplate> *constructor = (Persistent<FunctionTemplate> *)(void *)ptrToConstructor;
546
+ return constructor->Get (isolate);
516
547
}
517
548
518
549
// //////////////////////////////////////////////////////////////////////////////////////////////////
0 commit comments