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 all commits
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
10 changes: 9 additions & 1 deletion Runtime/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class SwiftRuntimeHeap {

allocHeap(value: any) {
const isObject = typeof value == "object";
const entry = this._heapEntryByValue.get(value);
const entry = this._heapEntryByValue.get(value);
if (isObject && entry) {
entry.rc++
return entry.id
Expand Down Expand Up @@ -362,6 +362,14 @@ 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)
return obj instanceof constructor
},
swjs_destroy_ref: (ref: ref) => {
this.heap.freeHeap(ref)
}
Expand Down
4 changes: 4 additions & 0 deletions Sources/JavaScriptKit/JSObject.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ public class JSObjectRef: Equatable {
getJSValue(this: self, index: Int32(index))
}

public func instanceof(_ constructor: JSFunctionRef) -> Bool {
_instanceof(self.id, constructor.id)
}

public subscript(_ index: Int) -> JSValue {
get { get(index) }
set { set(index, newValue) }
Expand Down
4 changes: 4 additions & 0 deletions Sources/JavaScriptKit/XcodeSupport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ import _CJavaScriptKit
_: UnsafePointer<RawJSValue>!, _: Int32,
_: UnsafeMutablePointer<JavaScriptObjectRef>!
) { fatalError() }
func _instanceof(
_: JavaScriptObjectRef,
_: JavaScriptObjectRef
) -> Bool { fatalError() }
func _create_function(
_: JavaScriptHostFuncRef,
_: UnsafePointer<JavaScriptObjectRef>!
Expand Down
6 changes: 6 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,11 @@ __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 bool
_instanceof(const JavaScriptObjectRef obj,
const JavaScriptObjectRef constructor);

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