From 2bb06942d9e5118191f4aa38e81b8c80a66fbb52 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Tue, 11 Jun 2024 20:14:01 +0900 Subject: [PATCH] Start migrating imported functions to the new definition style (#252) * Start migrating imported functions to the new definition style Use of different function names for C-name and Wasm's import name is a bit confusing. Also use of unprefixed function names in the C code is not a good practice. This commit starts migrating imported functions to the new naming convention and removes duplicated function definitions just for IDE support by using macro. * Migrate rest of imported functions * Migrate JavaScriptBigIntSupport module --- .../JSBigInt+I64.swift | 8 +- .../XcodeSupport.swift | 11 - .../JavaScriptEventLoop.swift | 7 +- .../BasicObjects/JSTypedArray.swift | 6 +- .../FundamentalObjects/JSBigInt.swift | 4 +- .../FundamentalObjects/JSClosure.swift | 4 +- .../FundamentalObjects/JSFunction.swift | 6 +- .../FundamentalObjects/JSObject.swift | 4 +- .../FundamentalObjects/JSString.swift | 10 +- .../JSThrowingFunction.swift | 6 +- Sources/JavaScriptKit/JSValue.swift | 12 +- Sources/JavaScriptKit/XcodeSupport.swift | 102 -------- .../include/_CJavaScriptKit+I64.h | 22 +- .../_CJavaScriptKit/include/_CJavaScriptKit.h | 219 ++++++++---------- 14 files changed, 136 insertions(+), 285 deletions(-) delete mode 100644 Sources/JavaScriptBigIntSupport/XcodeSupport.swift delete mode 100644 Sources/JavaScriptKit/XcodeSupport.swift diff --git a/Sources/JavaScriptBigIntSupport/JSBigInt+I64.swift b/Sources/JavaScriptBigIntSupport/JSBigInt+I64.swift index 4c8b9bca..ef868bf1 100644 --- a/Sources/JavaScriptBigIntSupport/JSBigInt+I64.swift +++ b/Sources/JavaScriptBigIntSupport/JSBigInt+I64.swift @@ -3,18 +3,18 @@ import _CJavaScriptBigIntSupport extension JSBigInt: JSBigIntExtended { public var int64Value: Int64 { - _bigint_to_i64(id, true) + swjs_bigint_to_i64(id, true) } public var uInt64Value: UInt64 { - UInt64(bitPattern: _bigint_to_i64(id, false)) + UInt64(bitPattern: swjs_bigint_to_i64(id, false)) } public convenience init(_ value: Int64) { - self.init(id: _i64_to_bigint(value, true)) + self.init(id: swjs_i64_to_bigint(value, true)) } public convenience init(unsigned value: UInt64) { - self.init(id: _i64_to_bigint(Int64(bitPattern: value), false)) + self.init(id: swjs_i64_to_bigint(Int64(bitPattern: value), false)) } } diff --git a/Sources/JavaScriptBigIntSupport/XcodeSupport.swift b/Sources/JavaScriptBigIntSupport/XcodeSupport.swift deleted file mode 100644 index 54912cec..00000000 --- a/Sources/JavaScriptBigIntSupport/XcodeSupport.swift +++ /dev/null @@ -1,11 +0,0 @@ -import _CJavaScriptKit - -/// Note: -/// Define all runtime function stubs which are imported from JavaScript environment. -/// SwiftPM doesn't support WebAssembly target yet, so we need to define them to -/// avoid link failure. -/// When running with JavaScript runtime library, they are ignored completely. -#if !arch(wasm32) - func _i64_to_bigint(_: Int64, _: Bool) -> JavaScriptObjectRef { fatalError() } - func _bigint_to_i64(_: JavaScriptObjectRef, _: Bool) -> Int64 { fatalError() } -#endif diff --git a/Sources/JavaScriptEventLoop/JavaScriptEventLoop.swift b/Sources/JavaScriptEventLoop/JavaScriptEventLoop.swift index 04aedb94..8f09279a 100644 --- a/Sources/JavaScriptEventLoop/JavaScriptEventLoop.swift +++ b/Sources/JavaScriptEventLoop/JavaScriptEventLoop.swift @@ -94,7 +94,7 @@ public final class JavaScriptEventLoop: SerialExecutor, @unchecked Sendable { #if compiler(>=5.9) typealias swift_task_asyncMainDrainQueue_hook_Fn = @convention(thin) (swift_task_asyncMainDrainQueue_original, swift_task_asyncMainDrainQueue_override) -> Void let swift_task_asyncMainDrainQueue_hook_impl: swift_task_asyncMainDrainQueue_hook_Fn = { _, _ in - _unsafe_event_loop_yield() + swjs_unsafe_event_loop_yield() } swift_task_asyncMainDrainQueue_hook = unsafeBitCast(swift_task_asyncMainDrainQueue_hook_impl, to: UnsafeMutableRawPointer?.self) #endif @@ -225,8 +225,3 @@ public extension JSPromise { } #endif - -// See `Sources/JavaScriptKit/XcodeSupport.swift` for rationale of the stub functions. -#if !arch(wasm32) - func _unsafe_event_loop_yield() { fatalError() } -#endif diff --git a/Sources/JavaScriptKit/BasicObjects/JSTypedArray.swift b/Sources/JavaScriptKit/BasicObjects/JSTypedArray.swift index 963419c9..6566e54f 100644 --- a/Sources/JavaScriptKit/BasicObjects/JSTypedArray.swift +++ b/Sources/JavaScriptKit/BasicObjects/JSTypedArray.swift @@ -47,7 +47,7 @@ public class JSTypedArray: JSBridgedClass, ExpressibleByArrayLiteral wh /// - Parameter array: The array that will be copied to create a new instance of TypedArray public convenience init(_ array: [Element]) { let jsArrayRef = array.withUnsafeBufferPointer { ptr in - _create_typed_array(Self.constructor!.id, ptr.baseAddress!, Int32(array.count)) + swjs_create_typed_array(Self.constructor!.id, ptr.baseAddress!, Int32(array.count)) } self.init(unsafelyWrapping: JSObject(id: jsArrayRef)) } @@ -82,7 +82,7 @@ public class JSTypedArray: JSBridgedClass, ExpressibleByArrayLiteral wh let rawBuffer = UnsafeMutableBufferPointer.allocate(capacity: bytesLength) defer { rawBuffer.deallocate() } let baseAddress = rawBuffer.baseAddress! - _load_typed_array(jsObject.id, baseAddress) + swjs_load_typed_array(jsObject.id, baseAddress) let length = bytesLength / MemoryLayout.size let rawBaseAddress = UnsafeRawPointer(baseAddress) let bufferPtr = UnsafeBufferPointer( @@ -113,7 +113,7 @@ public class JSTypedArray: JSBridgedClass, ExpressibleByArrayLiteral wh let rawBuffer = UnsafeMutableBufferPointer.allocate(capacity: bytesLength) defer { rawBuffer.deallocate() } let baseAddress = rawBuffer.baseAddress! - _load_typed_array(jsObject.id, baseAddress) + swjs_load_typed_array(jsObject.id, baseAddress) let length = bytesLength / MemoryLayout.size let rawBaseAddress = UnsafeRawPointer(baseAddress) let bufferPtr = UnsafeBufferPointer( diff --git a/Sources/JavaScriptKit/FundamentalObjects/JSBigInt.swift b/Sources/JavaScriptKit/FundamentalObjects/JSBigInt.swift index 104d194e..f3687246 100644 --- a/Sources/JavaScriptKit/FundamentalObjects/JSBigInt.swift +++ b/Sources/JavaScriptKit/FundamentalObjects/JSBigInt.swift @@ -15,13 +15,13 @@ public final class JSBigInt: JSObject { /// This doesn't require [JS-BigInt-integration](https://github.com/WebAssembly/JS-BigInt-integration) feature. public init(_slowBridge value: Int64) { let value = UInt64(bitPattern: value) - super.init(id: _i64_to_bigint_slow(UInt32(value & 0xffffffff), UInt32(value >> 32), true)) + super.init(id: swjs_i64_to_bigint_slow(UInt32(value & 0xffffffff), UInt32(value >> 32), true)) } /// Instantiate a new `JSBigInt` with given UInt64 value in a slow path /// This doesn't require [JS-BigInt-integration](https://github.com/WebAssembly/JS-BigInt-integration) feature. public init(_slowBridge value: UInt64) { - super.init(id: _i64_to_bigint_slow(UInt32(value & 0xffffffff), UInt32(value >> 32), false)) + super.init(id: swjs_i64_to_bigint_slow(UInt32(value & 0xffffffff), UInt32(value >> 32), false)) } override public var jsValue: JSValue { diff --git a/Sources/JavaScriptKit/FundamentalObjects/JSClosure.swift b/Sources/JavaScriptKit/FundamentalObjects/JSClosure.swift index 441dd2a6..6decbc81 100644 --- a/Sources/JavaScriptKit/FundamentalObjects/JSClosure.swift +++ b/Sources/JavaScriptKit/FundamentalObjects/JSClosure.swift @@ -22,7 +22,7 @@ public class JSOneshotClosure: JSObject, JSClosureProtocol { // 2. Create a new JavaScript function which calls the given Swift function. hostFuncRef = JavaScriptHostFuncRef(bitPattern: ObjectIdentifier(self)) id = withExtendedLifetime(JSString(file)) { file in - _create_function(hostFuncRef, line, file.asInternalJSRef()) + swjs_create_function(hostFuncRef, line, file.asInternalJSRef()) } // 3. Retain the given body in static storage by `funcRef`. @@ -88,7 +88,7 @@ public class JSClosure: JSFunction, JSClosureProtocol { // 2. Create a new JavaScript function which calls the given Swift function. hostFuncRef = JavaScriptHostFuncRef(bitPattern: ObjectIdentifier(self)) id = withExtendedLifetime(JSString(file)) { file in - _create_function(hostFuncRef, line, file.asInternalJSRef()) + swjs_create_function(hostFuncRef, line, file.asInternalJSRef()) } // 3. Retain the given body in static storage by `funcRef`. diff --git a/Sources/JavaScriptKit/FundamentalObjects/JSFunction.swift b/Sources/JavaScriptKit/FundamentalObjects/JSFunction.swift index 1de95fd3..54314613 100644 --- a/Sources/JavaScriptKit/FundamentalObjects/JSFunction.swift +++ b/Sources/JavaScriptKit/FundamentalObjects/JSFunction.swift @@ -55,7 +55,7 @@ public class JSFunction: JSObject { public func new(arguments: [ConvertibleToJSValue]) -> JSObject { arguments.withRawJSValues { rawValues in rawValues.withUnsafeBufferPointer { bufferPointer in - JSObject(id: _call_new(self.id, bufferPointer.baseAddress!, Int32(bufferPointer.count))) + JSObject(id: swjs_call_new(self.id, bufferPointer.baseAddress!, Int32(bufferPointer.count))) } } } @@ -101,7 +101,7 @@ public class JSFunction: JSObject { let argc = bufferPointer.count var payload1 = JavaScriptPayload1() var payload2 = JavaScriptPayload2() - let resultBitPattern = _call_function_no_catch( + let resultBitPattern = swjs_call_function_no_catch( id, argv, Int32(argc), &payload1, &payload2 ) @@ -121,7 +121,7 @@ public class JSFunction: JSObject { let argc = bufferPointer.count var payload1 = JavaScriptPayload1() var payload2 = JavaScriptPayload2() - let resultBitPattern = _call_function_with_this_no_catch(this.id, + let resultBitPattern = swjs_call_function_with_this_no_catch(this.id, id, argv, Int32(argc), &payload1, &payload2 ) diff --git a/Sources/JavaScriptKit/FundamentalObjects/JSObject.swift b/Sources/JavaScriptKit/FundamentalObjects/JSObject.swift index 04e7f3d5..1883cbf3 100644 --- a/Sources/JavaScriptKit/FundamentalObjects/JSObject.swift +++ b/Sources/JavaScriptKit/FundamentalObjects/JSObject.swift @@ -130,7 +130,7 @@ public class JSObject: Equatable { /// - Parameter constructor: The constructor function to check. /// - Returns: The result of `instanceof` in the JavaScript environment. public func isInstanceOf(_ constructor: JSFunction) -> Bool { - _instanceof(id, constructor.id) + swjs_instanceof(id, constructor.id) } static let _JS_Predef_Value_Global: JavaScriptObjectRef = 0 @@ -139,7 +139,7 @@ public class JSObject: Equatable { /// This allows access to the global properties and global names by accessing the `JSObject` returned. public static let global = JSObject(id: _JS_Predef_Value_Global) - deinit { _release(id) } + deinit { swjs_release(id) } /// Returns a Boolean value indicating whether two values point to same objects. /// diff --git a/Sources/JavaScriptKit/FundamentalObjects/JSString.swift b/Sources/JavaScriptKit/FundamentalObjects/JSString.swift index 5621793d..ee902f3e 100644 --- a/Sources/JavaScriptKit/FundamentalObjects/JSString.swift +++ b/Sources/JavaScriptKit/FundamentalObjects/JSString.swift @@ -23,20 +23,20 @@ public struct JSString: LosslessStringConvertible, Equatable { lazy var jsRef: JavaScriptObjectRef = { self.shouldDealocateRef = true return buffer.withUTF8 { bufferPtr in - return _decode_string(bufferPtr.baseAddress!, Int32(bufferPtr.count)) + return swjs_decode_string(bufferPtr.baseAddress!, Int32(bufferPtr.count)) } }() lazy var buffer: String = { var bytesRef: JavaScriptObjectRef = 0 - let bytesLength = Int(_encode_string(jsRef, &bytesRef)) + let bytesLength = Int(swjs_encode_string(jsRef, &bytesRef)) // +1 for null terminator let buffer = malloc(Int(bytesLength + 1))!.assumingMemoryBound(to: UInt8.self) defer { free(buffer) - _release(bytesRef) + swjs_release(bytesRef) } - _load_string(bytesRef, buffer) + swjs_load_string(bytesRef, buffer) buffer[bytesLength] = 0 return String(decodingCString: UnsafePointer(buffer), as: UTF8.self) }() @@ -52,7 +52,7 @@ public struct JSString: LosslessStringConvertible, Equatable { deinit { guard shouldDealocateRef else { return } - _release(jsRef) + swjs_release(jsRef) } } diff --git a/Sources/JavaScriptKit/FundamentalObjects/JSThrowingFunction.swift b/Sources/JavaScriptKit/FundamentalObjects/JSThrowingFunction.swift index 70589900..4763a877 100644 --- a/Sources/JavaScriptKit/FundamentalObjects/JSThrowingFunction.swift +++ b/Sources/JavaScriptKit/FundamentalObjects/JSThrowingFunction.swift @@ -44,7 +44,7 @@ public class JSThrowingFunction { var exceptionKind = JavaScriptValueKindAndFlags() var exceptionPayload1 = JavaScriptPayload1() var exceptionPayload2 = JavaScriptPayload2() - let resultObj = _call_throwing_new( + let resultObj = swjs_call_throwing_new( self.base.id, argv, Int32(argc), &exceptionKind, &exceptionPayload1, &exceptionPayload2 ) @@ -73,13 +73,13 @@ private func invokeJSFunction(_ jsFunc: JSFunction, arguments: [ConvertibleToJSV var payload1 = JavaScriptPayload1() var payload2 = JavaScriptPayload2() if let thisId = this?.id { - let resultBitPattern = _call_function_with_this( + let resultBitPattern = swjs_call_function_with_this( thisId, id, argv, Int32(argc), &payload1, &payload2 ) kindAndFlags = unsafeBitCast(resultBitPattern, to: JavaScriptValueKindAndFlags.self) } else { - let resultBitPattern = _call_function( + let resultBitPattern = swjs_call_function( id, argv, Int32(argc), &payload1, &payload2 ) diff --git a/Sources/JavaScriptKit/JSValue.swift b/Sources/JavaScriptKit/JSValue.swift index 58b28e07..85227614 100644 --- a/Sources/JavaScriptKit/JSValue.swift +++ b/Sources/JavaScriptKit/JSValue.swift @@ -196,7 +196,7 @@ extension JSValue: ExpressibleByNilLiteral { public func getJSValue(this: JSObject, name: JSString) -> JSValue { var rawValue = RawJSValue() - let rawBitPattern = _get_prop( + let rawBitPattern = swjs_get_prop( this.id, name.asInternalJSRef(), &rawValue.payload1, &rawValue.payload2 ) @@ -206,13 +206,13 @@ public func getJSValue(this: JSObject, name: JSString) -> JSValue { public func setJSValue(this: JSObject, name: JSString, value: JSValue) { value.withRawJSValue { rawValue in - _set_prop(this.id, name.asInternalJSRef(), rawValue.kind, rawValue.payload1, rawValue.payload2) + swjs_set_prop(this.id, name.asInternalJSRef(), rawValue.kind, rawValue.payload1, rawValue.payload2) } } public func getJSValue(this: JSObject, index: Int32) -> JSValue { var rawValue = RawJSValue() - let rawBitPattern = _get_subscript( + let rawBitPattern = swjs_get_subscript( this.id, index, &rawValue.payload1, &rawValue.payload2 ) @@ -222,7 +222,7 @@ public func getJSValue(this: JSObject, index: Int32) -> JSValue { public func setJSValue(this: JSObject, index: Int32, value: JSValue) { value.withRawJSValue { rawValue in - _set_subscript(this.id, index, + swjs_set_subscript(this.id, index, rawValue.kind, rawValue.payload1, rawValue.payload2) } @@ -230,7 +230,7 @@ public func setJSValue(this: JSObject, index: Int32, value: JSValue) { public func getJSValue(this: JSObject, symbol: JSSymbol) -> JSValue { var rawValue = RawJSValue() - let rawBitPattern = _get_prop( + let rawBitPattern = swjs_get_prop( this.id, symbol.id, &rawValue.payload1, &rawValue.payload2 ) @@ -240,7 +240,7 @@ public func getJSValue(this: JSObject, symbol: JSSymbol) -> JSValue { public func setJSValue(this: JSObject, symbol: JSSymbol, value: JSValue) { value.withRawJSValue { rawValue in - _set_prop(this.id, symbol.id, rawValue.kind, rawValue.payload1, rawValue.payload2) + swjs_set_prop(this.id, symbol.id, rawValue.kind, rawValue.payload1, rawValue.payload2) } } diff --git a/Sources/JavaScriptKit/XcodeSupport.swift b/Sources/JavaScriptKit/XcodeSupport.swift deleted file mode 100644 index ac5f117b..00000000 --- a/Sources/JavaScriptKit/XcodeSupport.swift +++ /dev/null @@ -1,102 +0,0 @@ -import _CJavaScriptKit - -/// Note: -/// Define stubs for runtime functions which are usually imported from JavaScript environment. -/// JavaScriptKit itself supports only WebAssembly target, but it should be able -/// to be built for host platforms like macOS or Linux for tentative IDE support. -/// (ideally, IDE should build for WebAssembly target though) -#if !arch(wasm32) - func _set_prop( - _: JavaScriptObjectRef, - _: JavaScriptObjectRef, - _: JavaScriptValueKind, - _: JavaScriptPayload1, - _: JavaScriptPayload2 - ) { fatalError() } - func _get_prop( - _: JavaScriptObjectRef, - _: JavaScriptObjectRef, - _: UnsafeMutablePointer!, - _: UnsafeMutablePointer! - ) -> UInt32 { fatalError() } - func _set_subscript( - _: JavaScriptObjectRef, - _: Int32, - _: JavaScriptValueKind, - _: JavaScriptPayload1, - _: JavaScriptPayload2 - ) { fatalError() } - func _get_subscript( - _: JavaScriptObjectRef, - _: Int32, - _: UnsafeMutablePointer!, - _: UnsafeMutablePointer! - ) -> UInt32 { fatalError() } - func _encode_string( - _: JavaScriptObjectRef, - _: UnsafeMutablePointer! - ) -> Int32 { fatalError() } - func _decode_string( - _: UnsafePointer!, - _: Int32 - ) -> JavaScriptObjectRef { fatalError() } - func _load_string( - _: JavaScriptObjectRef, - _: UnsafeMutablePointer! - ) { fatalError() } - func _i64_to_bigint_slow( - _: UInt32, _: UInt32, _: Bool - ) -> JavaScriptObjectRef { fatalError() } - func _call_function( - _: JavaScriptObjectRef, - _: UnsafePointer!, _: Int32, - _: UnsafeMutablePointer!, - _: UnsafeMutablePointer! - ) -> UInt32 { fatalError() } - func _call_function_no_catch( - _: JavaScriptObjectRef, - _: UnsafePointer!, _: Int32, - _: UnsafeMutablePointer!, - _: UnsafeMutablePointer! - ) -> UInt32 { fatalError() } - func _call_function_with_this( - _: JavaScriptObjectRef, - _: JavaScriptObjectRef, - _: UnsafePointer!, _: Int32, - _: UnsafeMutablePointer!, - _: UnsafeMutablePointer! - ) -> UInt32 { fatalError() } - func _call_function_with_this_no_catch( - _: JavaScriptObjectRef, - _: JavaScriptObjectRef, - _: UnsafePointer!, _: Int32, - _: UnsafeMutablePointer!, - _: UnsafeMutablePointer! - ) -> UInt32 { fatalError() } - func _call_new( - _: JavaScriptObjectRef, - _: UnsafePointer!, _: Int32 - ) -> JavaScriptObjectRef { fatalError() } - func _call_throwing_new( - _: JavaScriptObjectRef, - _: UnsafePointer!, _: Int32, - _: UnsafeMutablePointer!, - _: UnsafeMutablePointer!, - _: UnsafeMutablePointer! - ) -> JavaScriptObjectRef { fatalError() } - func _instanceof( - _: JavaScriptObjectRef, - _: JavaScriptObjectRef - ) -> Bool { fatalError() } - func _create_function(_: JavaScriptHostFuncRef, _: UInt32, _: JavaScriptObjectRef) -> JavaScriptObjectRef { fatalError() } - func _create_typed_array( - _: JavaScriptObjectRef, - _: UnsafePointer, - _: Int32 - ) -> JavaScriptObjectRef { fatalError() } - func _load_typed_array( - _: JavaScriptObjectRef, - _: UnsafeMutablePointer! - ) { fatalError() } - func _release(_: JavaScriptObjectRef) { fatalError() } -#endif diff --git a/Sources/_CJavaScriptBigIntSupport/include/_CJavaScriptKit+I64.h b/Sources/_CJavaScriptBigIntSupport/include/_CJavaScriptKit+I64.h index a04be1c2..69d25e47 100644 --- a/Sources/_CJavaScriptBigIntSupport/include/_CJavaScriptKit+I64.h +++ b/Sources/_CJavaScriptBigIntSupport/include/_CJavaScriptKit+I64.h @@ -4,24 +4,26 @@ #include <_CJavaScriptKit.h> +#if __wasm32__ +# define IMPORT_JS_FUNCTION(name, returns, args) \ +__attribute__((__import_module__("javascript_kit"), __import_name__(#name))) extern returns name args; +#else +# define IMPORT_JS_FUNCTION(name, returns, args) \ + static inline returns name args { \ + abort(); \ + } +#endif + /// Converts the provided Int64 or UInt64 to a BigInt. /// /// @param value The value to convert. /// @param is_signed Whether to treat the value as a signed integer or not. -#if __wasi__ -__attribute__((__import_module__("javascript_kit"), - __import_name__("swjs_i64_to_bigint"))) -#endif -extern JavaScriptObjectRef _i64_to_bigint(const long long value, bool is_signed); +IMPORT_JS_FUNCTION(swjs_i64_to_bigint, JavaScriptObjectRef, (const long long value, bool is_signed)) /// Converts the provided BigInt to an Int64 or UInt64. /// /// @param ref The target JavaScript object. /// @param is_signed Whether to treat the return value as a signed integer or not. -#if __wasi__ -__attribute__((__import_module__("javascript_kit"), - __import_name__("swjs_bigint_to_i64"))) -#endif -extern long long _bigint_to_i64(const JavaScriptObjectRef ref, bool is_signed); +IMPORT_JS_FUNCTION(swjs_bigint_to_i64, long long, (const JavaScriptObjectRef ref, bool is_signed)) #endif /* _CJavaScriptBigIntSupport_h */ diff --git a/Sources/_CJavaScriptKit/include/_CJavaScriptKit.h b/Sources/_CJavaScriptKit/include/_CJavaScriptKit.h index a9d8738a..431b8361 100644 --- a/Sources/_CJavaScriptKit/include/_CJavaScriptKit.h +++ b/Sources/_CJavaScriptKit/include/_CJavaScriptKit.h @@ -73,95 +73,86 @@ typedef struct { } RawJSValue; #if __wasm32__ +# define IMPORT_JS_FUNCTION(name, returns, args) \ +__attribute__((__import_module__("javascript_kit"), __import_name__(#name))) extern returns name args; +#else +# define IMPORT_JS_FUNCTION(name, returns, args) \ + static inline returns name args { \ + abort(); \ + } +#endif -/// `_set_prop` sets a value of `_this` JavaScript object. +/// Sets a value of `_this` JavaScript object. /// /// @param _this The target JavaScript object to set the given value. /// @param prop A JavaScript string object to reference a member of `_this` object. /// @param kind A kind of JavaScript value to set the target object. /// @param payload1 The first payload of JavaScript value to set the target object. /// @param payload2 The second payload of JavaScript value to set the target object. -__attribute__((__import_module__("javascript_kit"), - __import_name__("swjs_set_prop"))) -extern void _set_prop(const JavaScriptObjectRef _this, - const JavaScriptObjectRef prop, - const JavaScriptValueKind kind, - const JavaScriptPayload1 payload1, - const JavaScriptPayload2 payload2); +IMPORT_JS_FUNCTION(swjs_set_prop, void, (const JavaScriptObjectRef _this, + const JavaScriptObjectRef prop, + const JavaScriptValueKind kind, + const JavaScriptPayload1 payload1, + const JavaScriptPayload2 payload2)) -/// `_get_prop` gets a value of `_this` JavaScript object. +/// Gets a value of `_this` JavaScript object. /// /// @param _this The target JavaScript object to get its member value. /// @param prop A JavaScript string object to reference a member of `_this` object. /// @param payload1 A result pointer of first payload of JavaScript value to set the target object. /// @param payload2 A result pointer of second payload of JavaScript value to set the target object. /// @return A `JavaScriptValueKind` bits represented as 32bit integer for the returned value. -__attribute__((__import_module__("javascript_kit"), - __import_name__("swjs_get_prop"))) -extern uint32_t _get_prop( - const JavaScriptObjectRef _this, - const JavaScriptObjectRef prop, - JavaScriptPayload1 *payload1, - JavaScriptPayload2 *payload2 -); +IMPORT_JS_FUNCTION(swjs_get_prop, uint32_t, (const JavaScriptObjectRef _this, + const JavaScriptObjectRef prop, + JavaScriptPayload1 *payload1, + JavaScriptPayload2 *payload2)) -/// `_set_subscript` sets a value of `_this` JavaScript object. +/// Sets a value of `_this` JavaScript object. /// /// @param _this The target JavaScript object to set its member value. /// @param index A subscript index to set value. /// @param kind A kind of JavaScript value to set the target object. /// @param payload1 The first payload of JavaScript value to set the target object. /// @param payload2 The second payload of JavaScript value to set the target object. -__attribute__((__import_module__("javascript_kit"), - __import_name__("swjs_set_subscript"))) -extern void _set_subscript(const JavaScriptObjectRef _this, - const int index, - const JavaScriptValueKind kind, - const JavaScriptPayload1 payload1, - const JavaScriptPayload2 payload2); +IMPORT_JS_FUNCTION(swjs_set_subscript, void, (const JavaScriptObjectRef _this, + const int index, + const JavaScriptValueKind kind, + const JavaScriptPayload1 payload1, + const JavaScriptPayload2 payload2)) -/// `_get_subscript` gets a value of `_this` JavaScript object. +/// Gets a value of `_this` JavaScript object. /// /// @param _this The target JavaScript object to get its member value. /// @param index A subscript index to get value. /// @param payload1 A result pointer of first payload of JavaScript value to get the target object. /// @param payload2 A result pointer of second payload of JavaScript value to get the target object. /// @return A `JavaScriptValueKind` bits represented as 32bit integer for the returned value. -__attribute__((__import_module__("javascript_kit"), - __import_name__("swjs_get_subscript"))) -extern uint32_t _get_subscript( - const JavaScriptObjectRef _this, - const int index, - JavaScriptPayload1 *payload1, - JavaScriptPayload2 *payload2 -); +/// get a value of `_this` JavaScript object. +IMPORT_JS_FUNCTION(swjs_get_subscript, uint32_t, (const JavaScriptObjectRef _this, + const int index, + JavaScriptPayload1 *payload1, + JavaScriptPayload2 *payload2)) -/// `_encode_string` encodes the `str_obj` to bytes sequence and returns the length of bytes. +/// Encodes the `str_obj` to bytes sequence and returns the length of bytes. /// /// @param str_obj A JavaScript string object ref to encode. /// @param bytes_result A result pointer of bytes sequence representation in JavaScript. /// This value will be used to load the actual bytes using `_load_string`. /// @result The length of bytes sequence. This value will be used to allocate Swift side string buffer to load the actual bytes. -__attribute__((__import_module__("javascript_kit"), - __import_name__("swjs_encode_string"))) -extern int _encode_string(const JavaScriptObjectRef str_obj, JavaScriptObjectRef *bytes_result); +IMPORT_JS_FUNCTION(swjs_encode_string, int, (const JavaScriptObjectRef str_obj, JavaScriptObjectRef *bytes_result)) -/// `_decode_string` decodes the given bytes sequence into JavaScript string object. +/// Decodes the given bytes sequence into JavaScript string object. /// /// @param bytes_ptr A `uint8_t` byte sequence to decode. /// @param length The length of `bytes_ptr`. /// @result The decoded JavaScript string object. -__attribute__((__import_module__("javascript_kit"), - __import_name__("swjs_decode_string"))) -extern JavaScriptObjectRef _decode_string(const unsigned char *bytes_ptr, const int length); +IMPORT_JS_FUNCTION(swjs_decode_string, JavaScriptObjectRef, (const unsigned char *bytes_ptr, const int length)) -/// `_load_string` loads the actual bytes sequence of `bytes` into `buffer` which is a Swift side memory address. +/// Loads the actual bytes sequence of `bytes` into `buffer` which is a Swift side memory address. /// /// @param bytes A bytes sequence representation in JavaScript to load. This value should be derived from `_encode_string`. /// @param buffer A Swift side string buffer to load the bytes. -__attribute__((__import_module__("javascript_kit"), - __import_name__("swjs_load_string"))) -extern void _load_string(const JavaScriptObjectRef bytes, unsigned char *buffer); +IMPORT_JS_FUNCTION(swjs_load_string, void, (const JavaScriptObjectRef bytes, unsigned char *buffer)) /// Converts the provided Int64 or UInt64 to a BigInt in slow path by splitting 64bit integer to two 32bit integers /// to avoid depending on [JS-BigInt-integration](https://github.com/WebAssembly/JS-BigInt-integration) feature @@ -169,11 +160,9 @@ extern void _load_string(const JavaScriptObjectRef bytes, unsigned char *buffer) /// @param lower The lower 32bit of the value to convert. /// @param upper The upper 32bit of the value to convert. /// @param is_signed Whether to treat the value as a signed integer or not. -__attribute__((__import_module__("javascript_kit"), - __import_name__("swjs_i64_to_bigint_slow"))) -extern JavaScriptObjectRef _i64_to_bigint_slow(unsigned int lower, unsigned int upper, bool is_signed); +IMPORT_JS_FUNCTION(swjs_i64_to_bigint_slow, JavaScriptObjectRef, (unsigned int lower, unsigned int upper, bool is_signed)) -/// `_call_function` calls JavaScript function with given arguments list. +/// Calls JavaScript function with given arguments list. /// /// @param ref The target JavaScript function to call. /// @param argv A list of `RawJSValue` arguments to apply. @@ -181,16 +170,13 @@ extern JavaScriptObjectRef _i64_to_bigint_slow(unsigned int lower, unsigned int /// @param result_payload1 A result pointer of first payload of JavaScript value of returned result or thrown exception. /// @param result_payload2 A result pointer of second payload of JavaScript value of returned result or thrown exception. /// @return A `JavaScriptValueKindAndFlags` bits represented as 32bit integer for the returned value. -__attribute__((__import_module__("javascript_kit"), - __import_name__("swjs_call_function"))) -extern uint32_t _call_function( - const JavaScriptObjectRef ref, const RawJSValue *argv, - const int argc, - JavaScriptPayload1 *result_payload1, - JavaScriptPayload2 *result_payload2 -); +IMPORT_JS_FUNCTION(swjs_call_function, uint32_t, (const JavaScriptObjectRef ref, + const RawJSValue *argv, + const int argc, + JavaScriptPayload1 *result_payload1, + JavaScriptPayload2 *result_payload2)) -/// `_call_function` calls JavaScript function with given arguments list without capturing any exception +/// Calls JavaScript function with given arguments list without capturing any exception /// /// @param ref The target JavaScript function to call. /// @param argv A list of `RawJSValue` arguments to apply. @@ -198,16 +184,13 @@ extern uint32_t _call_function( /// @param result_payload1 A result pointer of first payload of JavaScript value of returned result or thrown exception. /// @param result_payload2 A result pointer of second payload of JavaScript value of returned result or thrown exception. /// @return A `JavaScriptValueKindAndFlags` bits represented as 32bit integer for the returned value. -__attribute__((__import_module__("javascript_kit"), - __import_name__("swjs_call_function_no_catch"))) -extern uint32_t _call_function_no_catch( - const JavaScriptObjectRef ref, const RawJSValue *argv, - const int argc, - JavaScriptPayload1 *result_payload1, - JavaScriptPayload2 *result_payload2 -); +IMPORT_JS_FUNCTION(swjs_call_function_no_catch, uint32_t, (const JavaScriptObjectRef ref, + const RawJSValue *argv, + const int argc, + JavaScriptPayload1 *result_payload1, + JavaScriptPayload2 *result_payload2)) -/// `_call_function_with_this` calls JavaScript function with given arguments list and given `_this`. +/// Calls JavaScript function with given arguments list and given `_this`. /// /// @param _this The value of `this` provided for the call to `func_ref`. /// @param func_ref The target JavaScript function to call. @@ -216,17 +199,14 @@ extern uint32_t _call_function_no_catch( /// @param result_payload1 A result pointer of first payload of JavaScript value of returned result or thrown exception. /// @param result_payload2 A result pointer of second payload of JavaScript value of returned result or thrown exception. /// @return A `JavaScriptValueKindAndFlags` bits represented as 32bit integer for the returned value. -__attribute__((__import_module__("javascript_kit"), - __import_name__("swjs_call_function_with_this"))) -extern uint32_t _call_function_with_this( - const JavaScriptObjectRef _this, - const JavaScriptObjectRef func_ref, - const RawJSValue *argv, const int argc, - JavaScriptPayload1 *result_payload1, - JavaScriptPayload2 *result_payload2 -); +IMPORT_JS_FUNCTION(swjs_call_function_with_this, uint32_t, (const JavaScriptObjectRef _this, + const JavaScriptObjectRef func_ref, + const RawJSValue *argv, + const int argc, + JavaScriptPayload1 *result_payload1, + JavaScriptPayload2 *result_payload2)) -/// `_call_function_with_this` calls JavaScript function with given arguments list and given `_this` without capturing any exception. +/// Calls JavaScript function with given arguments list and given `_this` without capturing any exception. /// /// @param _this The value of `this` provided for the call to `func_ref`. /// @param func_ref The target JavaScript function to call. @@ -235,28 +215,24 @@ extern uint32_t _call_function_with_this( /// @param result_payload1 A result pointer of first payload of JavaScript value of returned result or thrown exception. /// @param result_payload2 A result pointer of second payload of JavaScript value of returned result or thrown exception. /// @return A `JavaScriptValueKindAndFlags` bits represented as 32bit integer for the returned value. -__attribute__((__import_module__("javascript_kit"), - __import_name__("swjs_call_function_with_this_no_catch"))) -extern uint32_t _call_function_with_this_no_catch( - const JavaScriptObjectRef _this, - const JavaScriptObjectRef func_ref, - const RawJSValue *argv, const int argc, - JavaScriptPayload1 *result_payload1, - JavaScriptPayload2 *result_payload2 -); +IMPORT_JS_FUNCTION(swjs_call_function_with_this_no_catch, uint32_t, (const JavaScriptObjectRef _this, + const JavaScriptObjectRef func_ref, + const RawJSValue *argv, + const int argc, + JavaScriptPayload1 *result_payload1, + JavaScriptPayload2 *result_payload2)) -/// `_call_new` calls JavaScript object constructor with given arguments list. +/// Calls JavaScript object constructor with given arguments list. /// /// @param ref The target JavaScript constructor to call. /// @param argv A list of `RawJSValue` arguments to apply. /// @param argc The length of `argv``. /// @returns A reference to the constructed object. -__attribute__((__import_module__("javascript_kit"), - __import_name__("swjs_call_new"))) -extern JavaScriptObjectRef _call_new(const JavaScriptObjectRef ref, - const RawJSValue *argv, const int argc); +IMPORT_JS_FUNCTION(swjs_call_new, JavaScriptObjectRef, (const JavaScriptObjectRef ref, + const RawJSValue *argv, + const int argc)) -/// `_call_throwing_new` calls JavaScript object constructor with given arguments list. +/// Calls JavaScript object constructor with given arguments list. /// /// @param ref The target JavaScript constructor to call. /// @param argv A list of `RawJSValue` arguments to apply. @@ -265,67 +241,58 @@ extern JavaScriptObjectRef _call_new(const JavaScriptObjectRef ref, /// @param exception_payload1 A result pointer of first payload of JavaScript value of thrown exception. /// @param exception_payload2 A result pointer of second payload of JavaScript value of thrown exception. /// @returns A reference to the constructed object. -__attribute__((__import_module__("javascript_kit"), - __import_name__("swjs_call_throwing_new"))) -extern JavaScriptObjectRef _call_throwing_new(const JavaScriptObjectRef ref, - const RawJSValue *argv, const int argc, - JavaScriptValueKindAndFlags *exception_kind, - JavaScriptPayload1 *exception_payload1, - JavaScriptPayload2 *exception_payload2); +IMPORT_JS_FUNCTION(swjs_call_throwing_new, JavaScriptObjectRef, (const JavaScriptObjectRef ref, + const RawJSValue *argv, + const int argc, + JavaScriptValueKindAndFlags *exception_kind, + JavaScriptPayload1 *exception_payload1, + JavaScriptPayload2 *exception_payload2)) -/// `_instanceof` acts like JavaScript `instanceof` operator. +/// Acts like JavaScript `instanceof` operator. /// /// @param obj The target object to check its prototype chain. /// @param constructor The `constructor` object to check against. /// @result Return `true` if `constructor` appears anywhere in the prototype chain of `obj`. Return `false` if not. -__attribute__((__import_module__("javascript_kit"), - __import_name__("swjs_instanceof"))) -extern bool _instanceof(const JavaScriptObjectRef obj, - const JavaScriptObjectRef constructor); +IMPORT_JS_FUNCTION(swjs_instanceof, bool, (const JavaScriptObjectRef obj, + const JavaScriptObjectRef constructor)) -/// `_create_function` creates a JavaScript thunk function that calls Swift side closure. +/// Creates a JavaScript thunk function that calls Swift side closure. /// See also comments on JSFunction.swift /// /// @param host_func_id The target Swift side function called by the created thunk function. /// @param line The line where the function is created. Will be used for diagnostics /// @param file The file name where the function is created. Will be used for diagnostics /// @returns A reference to the newly-created JavaScript thunk function -__attribute__((__import_module__("javascript_kit"), - __import_name__("swjs_create_function"))) -extern JavaScriptObjectRef _create_function(const JavaScriptHostFuncRef host_func_id, - unsigned int line, JavaScriptObjectRef file); +IMPORT_JS_FUNCTION(swjs_create_function, JavaScriptObjectRef, (const JavaScriptHostFuncRef host_func_id, + unsigned int line, + JavaScriptObjectRef file)) -/// Instantiate a new `TypedArray` object with given elements +/// Instantiates a new `TypedArray` object with given elements /// This is used to provide an efficient way to create `TypedArray`. /// /// @param constructor The `TypedArray` constructor. /// @param elements_ptr The elements pointer to initialize. They are assumed to be the same size of `constructor` elements size. /// @param length The length of `elements_ptr` /// @returns A reference to the constructed typed array -__attribute__((__import_module__("javascript_kit"), - __import_name__("swjs_create_typed_array"))) -extern JavaScriptObjectRef _create_typed_array(const JavaScriptObjectRef constructor, - const void *elements_ptr, const int length); +IMPORT_JS_FUNCTION(swjs_create_typed_array, JavaScriptObjectRef, (const JavaScriptObjectRef constructor, + const void *elements_ptr, + const int length)) /// Copies the byte contents of a typed array into a Swift side memory buffer. /// /// @param ref A JavaScript typed array object. /// @param buffer A Swift side buffer into which to copy the bytes. -__attribute__((__import_module__("javascript_kit"), - __import_name__("swjs_load_typed_array"))) -extern void _load_typed_array(const JavaScriptObjectRef ref, unsigned char *buffer); +IMPORT_JS_FUNCTION(swjs_load_typed_array, void, (const JavaScriptObjectRef ref, unsigned char *buffer)) /// Decrements reference count of `ref` retained by `SwiftRuntimeHeap` in JavaScript side. /// /// @param ref The target JavaScript object. -__attribute__((__import_module__("javascript_kit"), - __import_name__("swjs_release"))) -extern void _release(const JavaScriptObjectRef ref); - -__attribute__((__import_module__("javascript_kit"), - __import_name__("swjs_unsafe_event_loop_yield"))) -extern void _unsafe_event_loop_yield(void); +IMPORT_JS_FUNCTION(swjs_release, void, (const JavaScriptObjectRef ref)) -#endif +/// Yields current program control by throwing `UnsafeEventLoopYield` JavaScript exception. +/// See note on `UnsafeEventLoopYield` for more details +/// +/// @note This function never returns +IMPORT_JS_FUNCTION(swjs_unsafe_event_loop_yield, void, (void)) #endif /* _CJavaScriptKit_h */