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 1f5486e

Browse files
committed
Add instanceof support
1 parent b5382e6 commit 1f5486e

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
@@ -362,6 +362,15 @@ 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+
const result = obj instanceof constructor
372+
writeUint32(result_ptr, Number(result))
373+
},
365374
swjs_destroy_ref: (ref: ref) => {
366375
this.heap.freeHeap(ref)
367376
}

‎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
@@ -64,6 +64,11 @@ import _CJavaScriptKit
6464
_: UnsafePointer<RawJSValue>!, _: Int32,
6565
_: UnsafeMutablePointer<JavaScriptObjectRef>!
6666
) { fatalError() }
67+
func _instanceof(
68+
_: JavaScriptObjectRef,
69+
_: JavaScriptObjectRef,
70+
_: UnsafeMutablePointer<Bool>!
71+
) { fatalError() }
6772
func _create_function(
6873
_: JavaScriptHostFuncRef,
6974
_: 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;
@@ -82,6 +83,12 @@ __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 void
88+
_instanceof(const JavaScriptObjectRef obj,
89+
const JavaScriptObjectRef constructor,
90+
bool *result);
91+
8592
__attribute__((__import_module__("javascript_kit"),
8693
__import_name__("swjs_create_function"))) extern void
8794
_create_function(const JavaScriptHostFuncRef host_func_id,

0 commit comments

Comments
(0)

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