Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e361221

Browse files
committed
Add instanceof support
1 parent a1cebd0 commit e361221

File tree

5 files changed

+33
-0
lines changed

5 files changed

+33
-0
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 & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,15 @@ export class SwiftRuntime {
336336
throw Error(`Invalid result type of object constructor of "${obj}": "${result}"`)
337337
writeUint32(result_obj, this.heap.allocHeap(result));
338338
},
339+
swjs_instanceof: (
340+
obj_ref: ref, constructor_ref: ref,
341+
result_ptr: pointer
342+
) => {
343+
const obj = this.heap.referenceHeap(obj_ref)
344+
const constructor = this.heap.referenceHeap(constructor_ref)
345+
const result = obj instanceof constructor
346+
writeUint32(result_ptr, Number(result))
347+
},
339348
swjs_destroy_ref: (ref: ref) => {
340349
this.heap.freeHeap(ref)
341350
}

‎Sources/JavaScriptKit/JSObject.swift‎

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

35+
public func instanceof(_ constructor: JSValue) -> Bool {
36+
instanceof(constructor.function!)
37+
}
38+
39+
public func instanceof(_ constructor: JSFunctionRef) -> Bool {
40+
var result: Bool = false
41+
_instanceof(self.id, constructor.id, &result)
42+
return result
43+
}
44+
3545
public subscript(_ index: Int) -> JSValue {
3646
get { get(index) }
3747
set { set(index, newValue) }

‎Sources/JavaScriptKit/XcodeSupport.swift‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ import _CJavaScriptKit
4646
_: UnsafePointer<RawJSValue>!, _: Int32,
4747
_: UnsafeMutablePointer<JavaScriptObjectRef>!
4848
) { fatalError() }
49+
func _instanceof(
50+
_: JavaScriptObjectRef,
51+
_: JavaScriptObjectRef,
52+
_: UnsafeMutablePointer<Bool>!
53+
) { fatalError() }
4954
func _create_function(
5055
_: JavaScriptHostFuncRef,
5156
_: UnsafePointer<JavaScriptObjectRef>!

‎Sources/_CJavaScriptKit/include/_CJavaScriptKit.h‎

Lines changed: 7 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;
@@ -69,6 +70,12 @@ __attribute__((__import_module__("javascript_kit"),
6970
_call_new(const JavaScriptObjectRef ref, const RawJSValue *argv, const int argc,
7071
JavaScriptObjectRef *result_obj);
7172

73+
__attribute__((__import_module__("javascript_kit"),
74+
__import_name__("swjs_instanceof"))) extern void
75+
_instanceof(const JavaScriptObjectRef obj,
76+
const JavaScriptObjectRef constructor,
77+
bool *result);
78+
7279
__attribute__((__import_module__("javascript_kit"),
7380
__import_name__("swjs_create_function"))) extern void
7481
_create_function(const JavaScriptHostFuncRef host_func_id,

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /