Skip to content

Add support for instanceof #25

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 3, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions IntegrationTests/TestSuites/Sources/PrimaryTests/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,8 @@ New_Object_Construction: do {
let cat1 = objectConstructor.new("Tama", 3, true)
try expectEqual(getJSValue(this: cat1, name: "name"), .string("Tama"))
try expectEqual(getJSValue(this: cat1, name: "age"), .number(3))
try expectEqual(cat1.instanceof(objectConstructor), true)
try expectEqual(cat1.instanceof(try expectFunction(getJSValue(this: .global, name: "Array"))), false)
let cat1Bark = try expectFunction(getJSValue(this: cat1, name: "bark"))
try expectEqual(cat1Bark(), .string("nyan"))

Expand Down
9 changes: 9 additions & 0 deletions Runtime/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,15 @@ export class SwiftRuntime {
throw Error(`Invalid result type of object constructor of "${obj}": "${result}"`)
writeUint32(result_obj, this.heap.allocHeap(result));
},
swjs_instanceof: (
obj_ref: ref, constructor_ref: ref,
result_ptr: pointer
) => {
const obj = this.heap.referenceHeap(obj_ref)
const constructor = this.heap.referenceHeap(constructor_ref)
const result = obj instanceof constructor
writeUint32(result_ptr, Number(result))
},
swjs_destroy_ref: (ref: ref) => {
this.heap.freeHeap(ref)
}
Expand Down
10 changes: 10 additions & 0 deletions Sources/JavaScriptKit/JSObject.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@ public class JSObjectRef: Equatable {
getJSValue(this: self, index: Int32(index))
}

public func instanceof(_ constructor: JSValue) -> Bool {
instanceof(constructor.function!)
}

public func instanceof(_ constructor: JSFunctionRef) -> Bool {
var result: Bool = false
_instanceof(self.id, constructor.id, &result)
return result
}

public subscript(_ index: Int) -> JSValue {
get { get(index) }
set { set(index, newValue) }
Expand Down
5 changes: 5 additions & 0 deletions Sources/JavaScriptKit/XcodeSupport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ import _CJavaScriptKit
_: UnsafePointer<RawJSValue>!, _: Int32,
_: UnsafeMutablePointer<JavaScriptObjectRef>!
) { fatalError() }
func _instanceof(
_: JavaScriptObjectRef,
_: JavaScriptObjectRef,
_: UnsafeMutablePointer<Bool>!
) { fatalError() }
func _create_function(
_: JavaScriptHostFuncRef,
_: UnsafePointer<JavaScriptObjectRef>!
Expand Down
7 changes: 7 additions & 0 deletions Sources/_CJavaScriptKit/include/_CJavaScriptKit.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define _CJavaScriptKit_h

#include <stdlib.h>
#include <stdbool.h>

typedef unsigned int JavaScriptObjectRef;
typedef unsigned int JavaScriptHostFuncRef;
Expand Down Expand Up @@ -82,6 +83,12 @@ __attribute__((__import_module__("javascript_kit"),
_call_new(const JavaScriptObjectRef ref, const RawJSValue *argv, const int argc,
JavaScriptObjectRef *result_obj);

__attribute__((__import_module__("javascript_kit"),
__import_name__("swjs_instanceof"))) extern void
_instanceof(const JavaScriptObjectRef obj,
const JavaScriptObjectRef constructor,
bool *result);

__attribute__((__import_module__("javascript_kit"),
__import_name__("swjs_create_function"))) extern void
_create_function(const JavaScriptHostFuncRef host_func_id,
Expand Down