Skip to content

Commit 1034940

Browse files
authored
Return from runtime functions instead of taking a pointer where possible (#147)
* Explicitly write false into the exception bit in swjs_call_throwing_new * Update Prettier * Return from runtime functions instead of taking a pointer where possible * Reorder functions to match each other * ); * bump versions
1 parent e2c14a1 commit 1034940

File tree

10 files changed

+112
-113
lines changed

10 files changed

+112
-113
lines changed

Runtime/src/index.ts

+53-33
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ export class SwiftRuntime {
141141
private instance: WebAssembly.Instance | null;
142142
private heap: SwiftRuntimeHeap;
143143
private _closureHeap: SwiftClosureHeap | null;
144-
private version: number = 702;
144+
private version: number = 703;
145145

146146
constructor() {
147147
this.instance = null;
@@ -154,7 +154,11 @@ export class SwiftRuntime {
154154
const exports = (this.instance
155155
.exports as any) as SwiftRuntimeExportedFunctions;
156156
if (exports.swjs_library_version() != this.version) {
157-
throw new Error(`The versions of JavaScriptKit are incompatible. ${exports.swjs_library_version()} != ${this.version}`);
157+
throw new Error(
158+
`The versions of JavaScriptKit are incompatible. ${exports.swjs_library_version()} != ${
159+
this.version
160+
}`
161+
);
158162
}
159163
}
160164
get closureHeap(): SwiftClosureHeap | null {
@@ -386,6 +390,7 @@ export class SwiftRuntime {
386390
decodeValue(kind, payload1, payload2)
387391
);
388392
},
393+
389394
swjs_get_prop: (
390395
ref: ref,
391396
name: ref,
@@ -397,6 +402,7 @@ export class SwiftRuntime {
397402
const result = Reflect.get(obj, readString(name));
398403
writeValue(result, kind_ptr, payload1_ptr, payload2_ptr, false);
399404
},
405+
400406
swjs_set_subscript: (
401407
ref: ref,
402408
index: number,
@@ -407,6 +413,7 @@ export class SwiftRuntime {
407413
const obj = this.heap.referenceHeap(ref);
408414
Reflect.set(obj, index, decodeValue(kind, payload1, payload2));
409415
},
416+
410417
swjs_get_subscript: (
411418
ref: ref,
412419
index: number,
@@ -418,12 +425,14 @@ export class SwiftRuntime {
418425
const result = Reflect.get(obj, index);
419426
writeValue(result, kind_ptr, payload1_ptr, payload2_ptr, false);
420427
},
428+
421429
swjs_encode_string: (ref: ref, bytes_ptr_result: pointer) => {
422430
const bytes = textEncoder.encode(this.heap.referenceHeap(ref));
423431
const bytes_ptr = this.heap.retain(bytes);
424432
writeUint32(bytes_ptr_result, bytes_ptr);
425433
return bytes.length;
426434
},
435+
427436
swjs_decode_string: (bytes_ptr: pointer, length: number) => {
428437
const uint8Memory = new Uint8Array(memory().buffer);
429438
const bytes = uint8Memory.subarray(
@@ -433,10 +442,12 @@ export class SwiftRuntime {
433442
const string = textDecoder.decode(bytes);
434443
return this.heap.retain(string);
435444
},
445+
436446
swjs_load_string: (ref: ref, buffer: pointer) => {
437447
const bytes = this.heap.referenceHeap(ref);
438448
writeString(buffer, bytes);
439449
},
450+
440451
swjs_call_function: (
441452
ref: ref,
442453
argv: pointer,
@@ -465,6 +476,7 @@ export class SwiftRuntime {
465476
}
466477
writeValue(result, kind_ptr, payload1_ptr, payload2_ptr, false);
467478
},
479+
468480
swjs_call_function_with_this: (
469481
obj_ref: ref,
470482
func_ref: ref,
@@ -491,33 +503,30 @@ export class SwiftRuntime {
491503
}
492504
writeValue(result, kind_ptr, payload1_ptr, payload2_ptr, false);
493505
},
494-
swjs_create_function: (
495-
host_func_id: number,
496-
func_ref_ptr: pointer
497-
) => {
498-
const func = function () {
499-
return callHostFunction(
500-
host_func_id,
501-
Array.prototype.slice.call(arguments)
502-
);
503-
};
504-
const func_ref = this.heap.retain(func);
505-
this.closureHeap?.alloc(func, func_ref);
506-
writeUint32(func_ref_ptr, func_ref);
506+
swjs_call_new: (ref: ref, argv: pointer, argc: number) => {
507+
const constructor = this.heap.referenceHeap(ref);
508+
const instance = Reflect.construct(
509+
constructor,
510+
decodeValues(argv, argc)
511+
);
512+
return this.heap.retain(instance);
507513
},
514+
508515
swjs_call_throwing_new: (
509516
ref: ref,
510517
argv: pointer,
511518
argc: number,
512-
result_obj: pointer,
513519
exception_kind_ptr: pointer,
514520
exception_payload1_ptr: pointer,
515521
exception_payload2_ptr: pointer
516522
) => {
517-
const obj = this.heap.referenceHeap(ref);
523+
const constructor = this.heap.referenceHeap(ref);
518524
let result: any;
519525
try {
520-
result = Reflect.construct(obj, decodeValues(argv, argc));
526+
result = Reflect.construct(
527+
constructor,
528+
decodeValues(argv, argc)
529+
);
521530
} catch (error) {
522531
writeValue(
523532
error,
@@ -526,30 +535,40 @@ export class SwiftRuntime {
526535
exception_payload2_ptr,
527536
true
528537
);
529-
return;
538+
return -1;
530539
}
531-
writeUint32(result_obj, this.heap.retain(result));
532-
},
533-
swjs_call_new: (
534-
ref: ref,
535-
argv: pointer,
536-
argc: number,
537-
result_obj: pointer
538-
) => {
539-
const obj = this.heap.referenceHeap(ref);
540-
const result = Reflect.construct(obj, decodeValues(argv, argc));
541-
writeUint32(result_obj, this.heap.retain(result));
540+
writeValue(
541+
null,
542+
exception_kind_ptr,
543+
exception_payload1_ptr,
544+
exception_payload2_ptr,
545+
false
546+
);
547+
return this.heap.retain(result);
542548
},
549+
543550
swjs_instanceof: (obj_ref: ref, constructor_ref: ref) => {
544551
const obj = this.heap.referenceHeap(obj_ref);
545552
const constructor = this.heap.referenceHeap(constructor_ref);
546553
return obj instanceof constructor;
547554
},
555+
556+
swjs_create_function: (host_func_id: number) => {
557+
const func = function () {
558+
return callHostFunction(
559+
host_func_id,
560+
Array.prototype.slice.call(arguments)
561+
);
562+
};
563+
const func_ref = this.heap.retain(func);
564+
this.closureHeap?.alloc(func, func_ref);
565+
return func_ref;
566+
},
567+
548568
swjs_create_typed_array: (
549569
constructor_ref: ref,
550570
elementsPtr: pointer,
551-
length: number,
552-
result_obj: pointer
571+
length: number
553572
) => {
554573
const ArrayType: TypedArray = this.heap.referenceHeap(
555574
constructor_ref
@@ -560,8 +579,9 @@ export class SwiftRuntime {
560579
length
561580
);
562581
// Call `.slice()` to copy the memory
563-
writeUint32(result_obj, this.heap.retain(array.slice()));
582+
return this.heap.retain(array.slice());
564583
},
584+
565585
swjs_release: (ref: ref) => {
566586
this.heap.release(ref);
567587
},

Sources/JavaScriptKit/BasicObjects/JSTypedArray.swift

+3-4
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,10 @@ public class JSTypedArray<Element>: JSBridgedClass, ExpressibleByArrayLiteral wh
4444
///
4545
/// - Parameter array: The array that will be copied to create a new instance of TypedArray
4646
public convenience init(_ array: [Element]) {
47-
var resultObj = JavaScriptObjectRef()
48-
array.withUnsafeBufferPointer { ptr in
49-
_create_typed_array(Element.typedArrayClass.id, ptr.baseAddress!, Int32(array.count), &resultObj)
47+
let jsArrayRef = array.withUnsafeBufferPointer { ptr in
48+
_create_typed_array(Element.typedArrayClass.id, ptr.baseAddress!, Int32(array.count))
5049
}
51-
self.init(unsafelyWrapping: JSObject(id: resultObj))
50+
self.init(unsafelyWrapping: JSObject(id: jsArrayRef))
5251
}
5352

5453
/// Convenience initializer for `Sequence`.

Sources/JavaScriptKit/FundamentalObjects/JSClosure.swift

+14-20
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,16 @@ public class JSOneshotClosure: JSObject, JSClosureProtocol {
1818
public init(_ body: @escaping ([JSValue]) -> JSValue) {
1919
// 1. Fill `id` as zero at first to access `self` to get `ObjectIdentifier`.
2020
super.init(id: 0)
21-
let objectId = ObjectIdentifier(self)
22-
let funcRef = JavaScriptHostFuncRef(bitPattern: Int32(objectId.hashValue))
23-
// 2. Retain the given body in static storage by `funcRef`.
24-
JSClosure.sharedClosures[funcRef] = (self, {
21+
22+
// 2. Create a new JavaScript function which calls the given Swift function.
23+
hostFuncRef = JavaScriptHostFuncRef(bitPattern: Int32(ObjectIdentifier(self).hashValue))
24+
id = _create_function(hostFuncRef)
25+
26+
// 3. Retain the given body in static storage by `funcRef`.
27+
JSClosure.sharedClosures[hostFuncRef] = (self, {
2528
defer { self.release() }
2629
return body($0)
2730
})
28-
// 3. Create a new JavaScript function which calls the given Swift function.
29-
var objectRef: JavaScriptObjectRef = 0
30-
_create_function(funcRef, &objectRef)
31-
32-
hostFuncRef = funcRef
33-
id = objectRef
3431
}
3532

3633
/// Release this function resource.
@@ -78,16 +75,13 @@ public class JSClosure: JSObject, JSClosureProtocol {
7875
public init(_ body: @escaping ([JSValue]) -> JSValue) {
7976
// 1. Fill `id` as zero at first to access `self` to get `ObjectIdentifier`.
8077
super.init(id: 0)
81-
let objectId = ObjectIdentifier(self)
82-
let funcRef = JavaScriptHostFuncRef(bitPattern: Int32(objectId.hashValue))
83-
// 2. Retain the given body in static storage by `funcRef`.
84-
Self.sharedClosures[funcRef] = (self, body)
85-
// 3. Create a new JavaScript function which calls the given Swift function.
86-
var objectRef: JavaScriptObjectRef = 0
87-
_create_function(funcRef, &objectRef)
88-
89-
hostFuncRef = funcRef
90-
id = objectRef
78+
79+
// 2. Create a new JavaScript function which calls the given Swift function.
80+
hostFuncRef = JavaScriptHostFuncRef(bitPattern: Int32(ObjectIdentifier(self).hashValue))
81+
id = _create_function(hostFuncRef)
82+
83+
// 3. Retain the given body in static storage by `funcRef`.
84+
Self.sharedClosures[hostFuncRef] = (self, body)
9185
}
9286

9387
#if JAVASCRIPTKIT_WITHOUT_WEAKREFS

Sources/JavaScriptKit/FundamentalObjects/JSFunction.swift

+1-5
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,7 @@ public class JSFunction: JSObject {
4141
public func new(arguments: [ConvertibleToJSValue]) -> JSObject {
4242
arguments.withRawJSValues { rawValues in
4343
rawValues.withUnsafeBufferPointer { bufferPointer in
44-
let argv = bufferPointer.baseAddress
45-
let argc = bufferPointer.count
46-
var resultObj = JavaScriptObjectRef()
47-
_call_new(self.id, argv, Int32(argc), &resultObj)
48-
return JSObject(id: resultObj)
44+
return JSObject(id: _call_new(self.id, bufferPointer.baseAddress!, Int32(bufferPointer.count)))
4945
}
5046
}
5147
}

Sources/JavaScriptKit/FundamentalObjects/JSThrowingFunction.swift

+2-3
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,9 @@ public class JSThrowingFunction {
4444
var exceptionKind = JavaScriptValueKindAndFlags()
4545
var exceptionPayload1 = JavaScriptPayload1()
4646
var exceptionPayload2 = JavaScriptPayload2()
47-
var resultObj = JavaScriptObjectRef()
48-
_call_throwing_new(
47+
let resultObj = _call_throwing_new(
4948
self.base.id, argv, Int32(argc),
50-
&resultObj, &exceptionKind, &exceptionPayload1, &exceptionPayload2
49+
&exceptionKind, &exceptionPayload1, &exceptionPayload2
5150
)
5251
if exceptionKind.isException {
5352
let exception = RawJSValue(kind: exceptionKind.kind, payload1: exceptionPayload1, payload2: exceptionPayload2)

Sources/JavaScriptKit/XcodeSupport.swift

+7-13
Original file line numberDiff line numberDiff line change
@@ -63,31 +63,25 @@ import _CJavaScriptKit
6363
) { fatalError() }
6464
func _call_new(
6565
_: JavaScriptObjectRef,
66-
_: UnsafePointer<RawJSValue>!, _: Int32,
67-
_: UnsafeMutablePointer<JavaScriptObjectRef>!
68-
) { fatalError() }
66+
_: UnsafePointer<RawJSValue>!, _: Int32
67+
) -> JavaScriptObjectRef { fatalError() }
6968
func _call_throwing_new(
7069
_: JavaScriptObjectRef,
7170
_: UnsafePointer<RawJSValue>!, _: Int32,
72-
_: UnsafeMutablePointer<JavaScriptObjectRef>!,
7371
_: UnsafeMutablePointer<JavaScriptValueKindAndFlags>!,
7472
_: UnsafeMutablePointer<JavaScriptPayload1>!,
7573
_: UnsafeMutablePointer<JavaScriptPayload2>!
76-
) { fatalError() }
74+
) -> JavaScriptObjectRef { fatalError() }
7775
func _instanceof(
7876
_: JavaScriptObjectRef,
7977
_: JavaScriptObjectRef
8078
) -> Bool { fatalError() }
81-
func _create_function(
82-
_: JavaScriptHostFuncRef,
83-
_: UnsafePointer<JavaScriptObjectRef>!
84-
) { fatalError() }
85-
func _release(_: JavaScriptObjectRef) { fatalError() }
79+
func _create_function(_: JavaScriptHostFuncRef) -> JavaScriptObjectRef { fatalError() }
8680
func _create_typed_array<T: TypedArrayElement>(
8781
_: JavaScriptObjectRef,
8882
_: UnsafePointer<T>,
89-
_: Int32,
90-
_: UnsafeMutablePointer<JavaScriptObjectRef>!
91-
) { fatalError() }
83+
_: Int32
84+
) -> JavaScriptObjectRef { fatalError() }
85+
func _release(_: JavaScriptObjectRef) { fatalError() }
9286

9387
#endif

Sources/_CJavaScriptKit/_CJavaScriptKit.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ void swjs_cleanup_host_function_call(void *argv_buffer) {
3636
/// this and `SwiftRuntime.version` in `./Runtime/src/index.ts`.
3737
__attribute__((export_name("swjs_library_version")))
3838
int swjs_library_version(void) {
39-
return 702;
39+
return 703;
4040
}
4141

4242
int _library_features(void);

0 commit comments

Comments
 (0)