1212#include < string.h> // memset, memcpy
1313#include < libplatform/libplatform.h>
1414#include < v8.h>
15+ #include " wrappers.h"
1516
1617using namespace v8 ;
1718
@@ -62,7 +63,7 @@ class GlobalValue {
6263extern " C" {
6364 ArrayBufferAllocator bufferAllocator;
6465
65- const void * initialize () {
66+ void * initialize () {
6667 V8::InitializeICU ();
6768 auto platform = platform::CreateDefaultPlatform ();
6869 V8::InitializePlatform (platform);
@@ -76,7 +77,7 @@ extern "C" {
7677 delete reinterpret_cast <Platform*>(platform);;
7778 }
7879
79- const void * createIsolate () {
80+ void * createIsolate () {
8081 Isolate::CreateParams create_params;
8182 create_params.array_buffer_allocator = &bufferAllocator;
8283 return Isolate::New (create_params);
@@ -86,12 +87,29 @@ extern "C" {
8687 reinterpret_cast <Isolate*>(isolate)->Dispose ();
8788 }
8889
89- void * createContext (void * isolatePtr) {
90+ void * createTemplate (void * isolatePtr) {
9091 auto isolate = reinterpret_cast <Isolate*>(isolatePtr);
9192 Locker isolateLocker (isolate);
9293 Isolate::Scope isolate_scope (isolate);
9394 HandleScope handle_scope (isolate);
94- Local<Context> context = Context::New (isolate);
95+ Local<ObjectTemplate> globalObject = ObjectTemplate::New (isolate);
96+ return new Global<ObjectTemplate>(isolate, globalObject);
97+ }
98+
99+ void disposeTemplate (void * context) {
100+ delete reinterpret_cast <Global<ObjectTemplate>*>(context);
101+ }
102+
103+ void * createContext (void * isolatePtr, void * templatePtr) {
104+ auto isolate = reinterpret_cast <Isolate*>(isolatePtr);
105+ auto globalGlobalTemplate = reinterpret_cast <Global<ObjectTemplate>*>(templatePtr);
106+
107+ Locker isolateLocker (isolate);
108+ Isolate::Scope isolate_scope (isolate);
109+ HandleScope handle_scope (isolate);
110+
111+ auto globalTemplate = globalGlobalTemplate->Get (isolate);
112+ Local<Context> context = Context::New (isolate, NULL , globalTemplate);
95113 return new Global<Context>(isolate, context);
96114 }
97115
@@ -173,4 +191,77 @@ extern "C" {
173191 GlobalValue scoped (isolate, value);
174192 return scoped->IsObject ();
175193 }
194+
195+ // MARK: functions
196+
197+ static void callback (const v8::FunctionCallbackInfo<v8::Value>& args) {
198+ int32_t id = args.Data ()->Int32Value ();
199+
200+ Isolate* isolate = args.GetIsolate ();
201+ HandleScope scope (isolate);
202+
203+ void * values[args.Length ()];
204+ for (int i = 0 ; i < args.Length (); i++) {
205+ values[i] = new Global<Value>(isolate, args[i]);
206+ }
207+ auto returnValue = args.GetReturnValue ();
208+ swiftCallback (isolate, id, values, args.Length (), &returnValue);
209+ }
210+
211+ void createFunction (void * isolatePtr, void * contextPtr, void * templatePtr, const char * namePtr, int32_t id) {
212+ auto isolate = reinterpret_cast <Isolate*>(isolatePtr);
213+ auto globalContext = reinterpret_cast <Global<Context>*>(contextPtr);
214+ auto globalGlobalTemplate = reinterpret_cast <Global<ObjectTemplate>*>(templatePtr);
215+
216+ Locker isolateLocker (isolate);
217+ Isolate::Scope isolate_scope (isolate);
218+ HandleScope handle_scope (isolate);
219+
220+ auto data = Integer::New (isolate, id);
221+ auto globalTemplate = globalGlobalTemplate->Get (isolate);
222+ globalTemplate->Set (
223+ String::NewFromUtf8 (isolate, namePtr),
224+ FunctionTemplate::New (isolate, callback, data));
225+
226+ auto context = globalContext->Get (isolate);
227+ auto globalObject = context->Global ();
228+ context->DetachGlobal ();
229+
230+ auto newContext = Context::New (isolate, NULL , globalTemplate, globalObject);
231+
232+ globalContext->Reset (isolate, newContext);
233+ }
234+
235+ void setReturnValueUndefined (void * isolatePtr, void * returnValuePtr) {
236+ auto returnValue = reinterpret_cast <ReturnValue<Value>*>(returnValuePtr);
237+ returnValue->SetUndefined ();
238+ }
239+
240+ void setReturnValueNull (void * isolatePtr, void * returnValuePtr) {
241+ auto returnValue = reinterpret_cast <ReturnValue<Value>*>(returnValuePtr);
242+ returnValue->SetNull ();
243+ }
244+
245+ void setReturnValueBoolean (void * isolatePtr, void * returnValuePtr, bool value) {
246+ auto returnValue = reinterpret_cast <ReturnValue<Value>*>(returnValuePtr);
247+ returnValue->Set (value);
248+ }
249+
250+ void setReturnValueNumber (void * isolatePtr, void * returnValuePtr, double value) {
251+ auto returnValue = reinterpret_cast <ReturnValue<Value>*>(returnValuePtr);
252+ returnValue->Set (value);
253+ }
254+
255+ void setReturnValueString (void * isolatePtr, void * returnValuePtr, const char * utf8) {
256+ auto isolate = reinterpret_cast <Isolate*>(isolatePtr);
257+ auto returnValue = reinterpret_cast <ReturnValue<Value>*>(returnValuePtr);
258+
259+ auto string = String::NewFromUtf8 (isolate, utf8);
260+ returnValue->Set (string);
261+ }
262+
263+ void setReturnValueEmptyString (void * isolatePtr, void * returnValuePtr) {
264+ auto returnValue = reinterpret_cast <ReturnValue<Value>*>(returnValuePtr);
265+ returnValue->SetEmptyString ();
266+ }
176267}
0 commit comments