Skip to content

Commit

Permalink
Start migrating imported functions to the new definition style (#252)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
kateinoigakukun authored Jun 11, 2024
1 parent 3a81371 commit 2bb0694
Show file tree
Hide file tree
Showing 14 changed files with 136 additions and 285 deletions.
8 changes: 4 additions & 4 deletions Sources/JavaScriptBigIntSupport/JSBigInt+I64.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}
11 changes: 0 additions & 11 deletions Sources/JavaScriptBigIntSupport/XcodeSupport.swift

This file was deleted.

7 changes: 1 addition & 6 deletions Sources/JavaScriptEventLoop/JavaScriptEventLoop.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
6 changes: 3 additions & 3 deletions Sources/JavaScriptKit/BasicObjects/JSTypedArray.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public class JSTypedArray<Element>: 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))
}
Expand Down Expand Up @@ -82,7 +82,7 @@ public class JSTypedArray<Element>: JSBridgedClass, ExpressibleByArrayLiteral wh
let rawBuffer = UnsafeMutableBufferPointer<UInt8>.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<Element>.size
let rawBaseAddress = UnsafeRawPointer(baseAddress)
let bufferPtr = UnsafeBufferPointer<Element>(
Expand Down Expand Up @@ -113,7 +113,7 @@ public class JSTypedArray<Element>: JSBridgedClass, ExpressibleByArrayLiteral wh
let rawBuffer = UnsafeMutableBufferPointer<UInt8>.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<Element>.size
let rawBaseAddress = UnsafeRawPointer(baseAddress)
let bufferPtr = UnsafeBufferPointer<Element>(
Expand Down
4 changes: 2 additions & 2 deletions Sources/JavaScriptKit/FundamentalObjects/JSBigInt.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions Sources/JavaScriptKit/FundamentalObjects/JSClosure.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down Expand Up @@ -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`.
Expand Down
6 changes: 3 additions & 3 deletions Sources/JavaScriptKit/FundamentalObjects/JSFunction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
}
}
}
Expand Down Expand Up @@ -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
)
Expand All @@ -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
)
Expand Down
4 changes: 2 additions & 2 deletions Sources/JavaScriptKit/FundamentalObjects/JSObject.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
///
Expand Down
10 changes: 5 additions & 5 deletions Sources/JavaScriptKit/FundamentalObjects/JSString.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}()
Expand All @@ -52,7 +52,7 @@ public struct JSString: LosslessStringConvertible, Equatable {

deinit {
guard shouldDealocateRef else { return }
_release(jsRef)
swjs_release(jsRef)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down Expand Up @@ -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
)
Expand Down
12 changes: 6 additions & 6 deletions Sources/JavaScriptKit/JSValue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand All @@ -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
)
Expand All @@ -222,15 +222,15 @@ 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)
}
}

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
)
Expand All @@ -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)
}
}

Expand Down
102 changes: 0 additions & 102 deletions Sources/JavaScriptKit/XcodeSupport.swift

This file was deleted.

Loading

0 comments on commit 2bb0694

Please sign in to comment.