Skip to content

Commit 3e9d02f

Browse files
committed
[V8] Implement object properties accessor
1 parent 4195ddd commit 3e9d02f

File tree

5 files changed

+59
-2
lines changed

5 files changed

+59
-2
lines changed

Sources/CV8/include/wrappers.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ extern "C" {
3535
// called from JSValue's destructor
3636
void disposeValue(void * _Nonnull pointer);
3737

38-
void * _Nullable evaluate(void * _Nonnull isolatePtr, void * _Nonnull contextPtr, const char* _Nonnull scriptPtr, void * _Nullable* _Nonnull exception);
38+
void * _Nullable evaluate(void * _Nonnull isolatePtr, void * _Nonnull contextPtr, const char* _Nonnull scriptPtr, void * _Nullable* _Nullable exception);
3939

4040
int getUtf8StringLength(void * _Nonnull isolatePtr, void * _Nonnull valuePtr);
4141
void copyUtf8String(void * _Nonnull isolatePtr, void * _Nonnull valuePtr, void * _Nonnull buffer, int count);
@@ -61,6 +61,8 @@ extern "C" {
6161
void setReturnValueString(void * _Nonnull isolatePtr, void * _Nonnull returnValuePtr, const char* _Nonnull utf8);
6262
void setReturnValueEmptyString(void * _Nonnull isolatePtr, void * _Nonnull returnValuePtr);
6363

64+
void * _Nullable getProperty(void * _Nonnull isolate, void * _Nonnull value, const char * _Nonnull key, void * _Nullable* _Nullable exception);
65+
6466

6567
#ifdef __cplusplus
6668
}

Sources/CV8/wrappers.cpp

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,9 @@ extern "C" {
132132
MaybeLocal<Value> result = script->Run(context);
133133

134134
if (result.IsEmpty()) {
135-
*exception = new Global<Value>(isolate, trycatch.Exception());
135+
if (exception != nullptr) {
136+
*exception = new Global<Value>(isolate, trycatch.Exception());
137+
}
136138
return nullptr;
137139
}
138140
auto local = result.ToLocalChecked();
@@ -264,4 +266,28 @@ extern "C" {
264266
auto returnValue = reinterpret_cast<ReturnValue<Value>*>(returnValuePtr);
265267
returnValue->SetEmptyString();
266268
}
269+
270+
// MARK: properties
271+
272+
void* getProperty(void* isolatePtr, void* valuePtr, const char* keyPtr, void** exception) {
273+
auto isolate = reinterpret_cast<Isolate*>(isolatePtr);
274+
auto value = reinterpret_cast<Global<Value>*>(valuePtr);
275+
276+
Locker isolateLocker(isolate);
277+
TryCatch trycatch(isolate);
278+
Isolate::Scope isolate_scope(isolate);
279+
HandleScope handle_scope(isolate);
280+
281+
auto object = value->Get(isolate)->ToObject();
282+
auto key = String::NewFromUtf8(isolate, keyPtr);
283+
auto result = object->GetRealNamedProperty(key);
284+
285+
if (result.IsEmpty()) {
286+
if (exception != nullptr) {
287+
*exception = new Global<Value>(isolate, trycatch.Exception());
288+
}
289+
return nullptr;
290+
}
291+
return new Global<Value>(isolate, result);
292+
}
267293
}

Sources/V8/JSValue.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,18 @@ extension JSValue: JavaScript.JSValue {
6363
}
6464
}
6565

66+
extension JSValue {
67+
subscript(_ key: String) -> JSValue? {
68+
guard isObject else {
69+
return nil
70+
}
71+
guard let result = getProperty(isolate, pointer, key, nil) else {
72+
return nil
73+
}
74+
return JSValue(isolate: isolate, pointer: result)
75+
}
76+
}
77+
6678
extension JSValue: CustomStringConvertible {
6779
public var description: String {
6880
return try! toString()

Tests/V8Tests/JSValueTests.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,4 +109,20 @@ final class JSValueTests: TestCase {
109109
fail(String(describing: error))
110110
}
111111
}
112+
113+
func testProperty() {
114+
do {
115+
let runtime = JSRuntime()
116+
let context = JSContext(runtime)
117+
let result = try context.evaluate("""
118+
(function(){
119+
return { property: 'test' }
120+
})()
121+
""")
122+
123+
assertEqual(try result["property"]?.toString(), "test")
124+
} catch {
125+
fail(String(describing: error))
126+
}
127+
}
112128
}

Tests/V8Tests/XCTestManifests.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ extension JSValueTests {
77
("testIsNumber", testIsNumber),
88
("testIsString", testIsString),
99
("testIsUndefined", testIsUndefined),
10+
("testProperty", testProperty),
1011
("testToInt", testToInt),
1112
("testToString", testToString),
1213
]

0 commit comments

Comments
 (0)