Skip to content

Commit 18adf42

Browse files
authored
Add support for instanceof (#25)
* Add instanceof support * Make instanceof safe * Directly return the result of instanceof * Remove instanceof(JSValue)
1 parent b5382e6 commit 18adf42

File tree

5 files changed

+25
-1
lines changed

5 files changed

+25
-1
lines changed

IntegrationTests/TestSuites/Sources/PrimaryTests/main.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,8 @@ New_Object_Construction: do {
228228
let cat1 = objectConstructor.new("Tama", 3, true)
229229
try expectEqual(getJSValue(this: cat1, name: "name"), .string("Tama"))
230230
try expectEqual(getJSValue(this: cat1, name: "age"), .number(3))
231+
try expectEqual(cat1.instanceof(objectConstructor), true)
232+
try expectEqual(cat1.instanceof(try expectFunction(getJSValue(this: .global, name: "Array"))), false)
231233
let cat1Bark = try expectFunction(getJSValue(this: cat1, name: "bark"))
232234
try expectEqual(cat1Bark(), .string("nyan"))
233235

Runtime/src/index.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class SwiftRuntimeHeap {
5959

6060
allocHeap(value: any) {
6161
const isObject = typeof value == "object";
62-
const entry = this._heapEntryByValue.get(value);
62+
const entry = this._heapEntryByValue.get(value);
6363
if (isObject && entry) {
6464
entry.rc++
6565
return entry.id
@@ -362,6 +362,14 @@ export class SwiftRuntime {
362362
throw Error(`Invalid result type of object constructor of "${obj}": "${result}"`)
363363
writeUint32(result_obj, this.heap.allocHeap(result));
364364
},
365+
swjs_instanceof: (
366+
obj_ref: ref, constructor_ref: ref,
367+
result_ptr: pointer
368+
) => {
369+
const obj = this.heap.referenceHeap(obj_ref)
370+
const constructor = this.heap.referenceHeap(constructor_ref)
371+
return obj instanceof constructor
372+
},
365373
swjs_destroy_ref: (ref: ref) => {
366374
this.heap.freeHeap(ref)
367375
}

Sources/JavaScriptKit/JSObject.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ public class JSObjectRef: Equatable {
3232
getJSValue(this: self, index: Int32(index))
3333
}
3434

35+
public func instanceof(_ constructor: JSFunctionRef) -> Bool {
36+
_instanceof(self.id, constructor.id)
37+
}
38+
3539
public subscript(_ index: Int) -> JSValue {
3640
get { get(index) }
3741
set { set(index, newValue) }

Sources/JavaScriptKit/XcodeSupport.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ import _CJavaScriptKit
6464
_: UnsafePointer<RawJSValue>!, _: Int32,
6565
_: UnsafeMutablePointer<JavaScriptObjectRef>!
6666
) { fatalError() }
67+
func _instanceof(
68+
_: JavaScriptObjectRef,
69+
_: JavaScriptObjectRef
70+
) -> Bool { fatalError() }
6771
func _create_function(
6872
_: JavaScriptHostFuncRef,
6973
_: UnsafePointer<JavaScriptObjectRef>!

Sources/_CJavaScriptKit/include/_CJavaScriptKit.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define _CJavaScriptKit_h
33

44
#include <stdlib.h>
5+
#include <stdbool.h>
56

67
typedef unsigned int JavaScriptObjectRef;
78
typedef unsigned int JavaScriptHostFuncRef;
@@ -82,6 +83,11 @@ __attribute__((__import_module__("javascript_kit"),
8283
_call_new(const JavaScriptObjectRef ref, const RawJSValue *argv, const int argc,
8384
JavaScriptObjectRef *result_obj);
8485

86+
__attribute__((__import_module__("javascript_kit"),
87+
__import_name__("swjs_instanceof"))) extern bool
88+
_instanceof(const JavaScriptObjectRef obj,
89+
const JavaScriptObjectRef constructor);
90+
8591
__attribute__((__import_module__("javascript_kit"),
8692
__import_name__("swjs_create_function"))) extern void
8793
_create_function(const JavaScriptHostFuncRef host_func_id,

0 commit comments

Comments
 (0)