Skip to content

Commit 58926d4

Browse files
committed
[V8] Set function to global prototype, preserve the context
1 parent a6b0e1e commit 58926d4

File tree

7 files changed

+45
-20
lines changed

7 files changed

+45
-20
lines changed

Sources/CV8/c_v8.cpp

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -172,27 +172,22 @@ extern "C" {
172172
swiftCallback(isolate, id, values, args.Length(), &returnValue);
173173
}
174174

175-
void createFunction(void* isolatePtr, void* contextPtr, void* templatePtr, const char* namePtr, int32_t id) {
175+
void createFunction(void* isolatePtr, void* contextPtr, const char* namePtr, int32_t id) {
176176
auto isolate = reinterpret_cast<Isolate*>(isolatePtr);
177177
auto globalContext = reinterpret_cast<Global<Context>*>(contextPtr);
178-
auto globalGlobalTemplate = reinterpret_cast<Global<ObjectTemplate>*>(templatePtr);
179178

180179
Locker isolateLocker(isolate);
181180
Isolate::Scope isolate_scope(isolate);
182181
HandleScope handle_scope(isolate);
183-
184-
auto data = Integer::New(isolate, id);
185-
auto globalTemplate = globalGlobalTemplate->Get(isolate);
186-
globalTemplate->Set(
187-
String::NewFromUtf8(isolate, namePtr),
188-
FunctionTemplate::New(isolate, callback, data));
189182

190183
auto context = globalContext->Get(isolate);
191-
auto globalObject = context->Global();
192-
context->DetachGlobal();
184+
Context::Scope context_scope(context);
193185

194-
auto newContext = Context::New(isolate, NULL, globalTemplate, globalObject);
195-
globalContext->Reset(isolate, newContext);
186+
auto data = Integer::New(isolate, id);
187+
auto name = String::NewFromUtf8(isolate, namePtr);
188+
auto functionTemplate = FunctionTemplate::New(isolate, callback, data);
189+
auto prototype = Local<Object>::Cast(context->Global()->GetPrototype());
190+
prototype->Set(name, functionTemplate->GetFunction());
196191
}
197192

198193
void setReturnValueUndefined(void* isolatePtr, void* returnValuePtr) {

Sources/CV8/include/c_v8.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ extern "C" {
4646

4747

4848
void (* _Nullable swiftCallback)(void * _Nonnull isolate, int32_t id, void * _Nullable * _Nonnull arguments, int32_t count, void * _Nonnull returnValue);
49-
void createFunction(void * _Nonnull isolatePtr, void * _Nonnull contextPtr, void * _Nonnull templatePtr, const char* _Nonnull namePtr, int32_t id);
49+
void createFunction(void * _Nonnull isolatePtr, void * _Nonnull contextPtr, const char* _Nonnull namePtr, int32_t id);
5050

5151
void setReturnValueUndefined(void * _Nonnull isolatePtr, void * _Nonnull returnValuePtr);
5252
void setReturnValueNull(void * _Nonnull isolatePtr, void * _Nonnull returnValuePtr);
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
#include <string.h> // memset, memcpy
1313
#include <libplatform/libplatform.h>
1414
#include <v8.h>
15-
#include "c_v8_platrofm.h"
15+
#include "c_v8_platform.h"
1616

1717
using namespace v8;
1818

Sources/CV8Platform/include/c_v8_platrofm.h renamed to Sources/CV8Platform/include/c_v8_platform.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
* See CONTRIBUTORS.txt for the list of the project authors
99
*/
1010

11-
#ifndef c_v8_platrofm_h
12-
#define c_v8_platrofm_h
11+
#ifndef c_v8_platform_h
12+
#define c_v8_platform_h
1313

1414
#include <stdint.h>
1515
#include <stdbool.h>
@@ -29,4 +29,4 @@ extern "C" {
2929
}
3030
#endif
3131

32-
#endif /* c_v8_platrofm_h */
32+
#endif /* c_v8_platform_h */

Sources/CV8Platform/include/module.modulemap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*/
1010

1111
module CV8Platform [system] {
12-
header "c_v8_platrofm.h"
12+
header "c_v8_platform.h"
1313
link "v8"
1414
export *
1515
}

Sources/V8API/JSContext.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,8 @@ public class JSContext {
1818

1919
public init(isolate: UnsafeMutableRawPointer) {
2020
self.isolate = isolate
21-
let template = CV8.createTemplate(isolate)
21+
self.template = CV8.createTemplate(isolate)
2222
self.context = CV8.createContext(isolate, template)
23-
self.template = template
2423
}
2524

2625
deinit {

Tests/V8Tests/V8Tests.swift

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,35 @@ final class V8Tests: TestCase {
105105
fail(String(describing: error))
106106
}
107107
}
108+
109+
func testPersistentContext() {
110+
do {
111+
let context = JSContext()
112+
try context.evaluate("result = 'success'")
113+
assertEqual(try context.evaluate("result").toString(), "success")
114+
115+
try context.createFunction(name: "test") { (arguments) -> Value in
116+
return .string("test ok")
117+
}
118+
119+
assertEqual(try context.evaluate("result").toString(), "success")
120+
} catch {
121+
fail(String(describing: error))
122+
}
123+
}
124+
125+
func testSandbox() {
126+
do {
127+
let context = JSContext()
128+
try context.evaluate("test = 'hello'")
129+
let result = try context.evaluate("test")
130+
assertEqual(try result.toString(), "hello")
131+
} catch {
132+
fail(String(describing: error))
133+
return
134+
}
135+
136+
let context = JSContext()
137+
assertThrowsError(try context.evaluate("test"))
138+
}
108139
}

0 commit comments

Comments
 (0)