Skip to content

Commit 2bb0694

Browse files
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
1 parent 3a81371 commit 2bb0694

File tree

14 files changed

+136
-285
lines changed

14 files changed

+136
-285
lines changed

Diff for: Sources/JavaScriptBigIntSupport/JSBigInt+I64.swift

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,18 @@ import _CJavaScriptBigIntSupport
33

44
extension JSBigInt: JSBigIntExtended {
55
public var int64Value: Int64 {
6-
_bigint_to_i64(id, true)
6+
swjs_bigint_to_i64(id, true)
77
}
88

99
public var uInt64Value: UInt64 {
10-
UInt64(bitPattern: _bigint_to_i64(id, false))
10+
UInt64(bitPattern: swjs_bigint_to_i64(id, false))
1111
}
1212

1313
public convenience init(_ value: Int64) {
14-
self.init(id: _i64_to_bigint(value, true))
14+
self.init(id: swjs_i64_to_bigint(value, true))
1515
}
1616

1717
public convenience init(unsigned value: UInt64) {
18-
self.init(id: _i64_to_bigint(Int64(bitPattern: value), false))
18+
self.init(id: swjs_i64_to_bigint(Int64(bitPattern: value), false))
1919
}
2020
}

Diff for: Sources/JavaScriptBigIntSupport/XcodeSupport.swift

-11
This file was deleted.

Diff for: Sources/JavaScriptEventLoop/JavaScriptEventLoop.swift

+1-6
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public final class JavaScriptEventLoop: SerialExecutor, @unchecked Sendable {
9494
#if compiler(>=5.9)
9595
typealias swift_task_asyncMainDrainQueue_hook_Fn = @convention(thin) (swift_task_asyncMainDrainQueue_original, swift_task_asyncMainDrainQueue_override) -> Void
9696
let swift_task_asyncMainDrainQueue_hook_impl: swift_task_asyncMainDrainQueue_hook_Fn = { _, _ in
97-
_unsafe_event_loop_yield()
97+
swjs_unsafe_event_loop_yield()
9898
}
9999
swift_task_asyncMainDrainQueue_hook = unsafeBitCast(swift_task_asyncMainDrainQueue_hook_impl, to: UnsafeMutableRawPointer?.self)
100100
#endif
@@ -225,8 +225,3 @@ public extension JSPromise {
225225
}
226226

227227
#endif
228-
229-
// See `Sources/JavaScriptKit/XcodeSupport.swift` for rationale of the stub functions.
230-
#if !arch(wasm32)
231-
func _unsafe_event_loop_yield() { fatalError() }
232-
#endif

Diff for: Sources/JavaScriptKit/BasicObjects/JSTypedArray.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public class JSTypedArray<Element>: JSBridgedClass, ExpressibleByArrayLiteral wh
4747
/// - Parameter array: The array that will be copied to create a new instance of TypedArray
4848
public convenience init(_ array: [Element]) {
4949
let jsArrayRef = array.withUnsafeBufferPointer { ptr in
50-
_create_typed_array(Self.constructor!.id, ptr.baseAddress!, Int32(array.count))
50+
swjs_create_typed_array(Self.constructor!.id, ptr.baseAddress!, Int32(array.count))
5151
}
5252
self.init(unsafelyWrapping: JSObject(id: jsArrayRef))
5353
}
@@ -82,7 +82,7 @@ public class JSTypedArray<Element>: JSBridgedClass, ExpressibleByArrayLiteral wh
8282
let rawBuffer = UnsafeMutableBufferPointer<UInt8>.allocate(capacity: bytesLength)
8383
defer { rawBuffer.deallocate() }
8484
let baseAddress = rawBuffer.baseAddress!
85-
_load_typed_array(jsObject.id, baseAddress)
85+
swjs_load_typed_array(jsObject.id, baseAddress)
8686
let length = bytesLength / MemoryLayout<Element>.size
8787
let rawBaseAddress = UnsafeRawPointer(baseAddress)
8888
let bufferPtr = UnsafeBufferPointer<Element>(
@@ -113,7 +113,7 @@ public class JSTypedArray<Element>: JSBridgedClass, ExpressibleByArrayLiteral wh
113113
let rawBuffer = UnsafeMutableBufferPointer<UInt8>.allocate(capacity: bytesLength)
114114
defer { rawBuffer.deallocate() }
115115
let baseAddress = rawBuffer.baseAddress!
116-
_load_typed_array(jsObject.id, baseAddress)
116+
swjs_load_typed_array(jsObject.id, baseAddress)
117117
let length = bytesLength / MemoryLayout<Element>.size
118118
let rawBaseAddress = UnsafeRawPointer(baseAddress)
119119
let bufferPtr = UnsafeBufferPointer<Element>(

Diff for: Sources/JavaScriptKit/FundamentalObjects/JSBigInt.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ public final class JSBigInt: JSObject {
1515
/// This doesn't require [JS-BigInt-integration](https://github.com/WebAssembly/JS-BigInt-integration) feature.
1616
public init(_slowBridge value: Int64) {
1717
let value = UInt64(bitPattern: value)
18-
super.init(id: _i64_to_bigint_slow(UInt32(value & 0xffffffff), UInt32(value >> 32), true))
18+
super.init(id: swjs_i64_to_bigint_slow(UInt32(value & 0xffffffff), UInt32(value >> 32), true))
1919
}
2020

2121
/// Instantiate a new `JSBigInt` with given UInt64 value in a slow path
2222
/// This doesn't require [JS-BigInt-integration](https://github.com/WebAssembly/JS-BigInt-integration) feature.
2323
public init(_slowBridge value: UInt64) {
24-
super.init(id: _i64_to_bigint_slow(UInt32(value & 0xffffffff), UInt32(value >> 32), false))
24+
super.init(id: swjs_i64_to_bigint_slow(UInt32(value & 0xffffffff), UInt32(value >> 32), false))
2525
}
2626

2727
override public var jsValue: JSValue {

Diff for: Sources/JavaScriptKit/FundamentalObjects/JSClosure.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public class JSOneshotClosure: JSObject, JSClosureProtocol {
2222
// 2. Create a new JavaScript function which calls the given Swift function.
2323
hostFuncRef = JavaScriptHostFuncRef(bitPattern: ObjectIdentifier(self))
2424
id = withExtendedLifetime(JSString(file)) { file in
25-
_create_function(hostFuncRef, line, file.asInternalJSRef())
25+
swjs_create_function(hostFuncRef, line, file.asInternalJSRef())
2626
}
2727

2828
// 3. Retain the given body in static storage by `funcRef`.
@@ -88,7 +88,7 @@ public class JSClosure: JSFunction, JSClosureProtocol {
8888
// 2. Create a new JavaScript function which calls the given Swift function.
8989
hostFuncRef = JavaScriptHostFuncRef(bitPattern: ObjectIdentifier(self))
9090
id = withExtendedLifetime(JSString(file)) { file in
91-
_create_function(hostFuncRef, line, file.asInternalJSRef())
91+
swjs_create_function(hostFuncRef, line, file.asInternalJSRef())
9292
}
9393

9494
// 3. Retain the given body in static storage by `funcRef`.

Diff for: Sources/JavaScriptKit/FundamentalObjects/JSFunction.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public class JSFunction: JSObject {
5555
public func new(arguments: [ConvertibleToJSValue]) -> JSObject {
5656
arguments.withRawJSValues { rawValues in
5757
rawValues.withUnsafeBufferPointer { bufferPointer in
58-
JSObject(id: _call_new(self.id, bufferPointer.baseAddress!, Int32(bufferPointer.count)))
58+
JSObject(id: swjs_call_new(self.id, bufferPointer.baseAddress!, Int32(bufferPointer.count)))
5959
}
6060
}
6161
}
@@ -101,7 +101,7 @@ public class JSFunction: JSObject {
101101
let argc = bufferPointer.count
102102
var payload1 = JavaScriptPayload1()
103103
var payload2 = JavaScriptPayload2()
104-
let resultBitPattern = _call_function_no_catch(
104+
let resultBitPattern = swjs_call_function_no_catch(
105105
id, argv, Int32(argc),
106106
&payload1, &payload2
107107
)
@@ -121,7 +121,7 @@ public class JSFunction: JSObject {
121121
let argc = bufferPointer.count
122122
var payload1 = JavaScriptPayload1()
123123
var payload2 = JavaScriptPayload2()
124-
let resultBitPattern = _call_function_with_this_no_catch(this.id,
124+
let resultBitPattern = swjs_call_function_with_this_no_catch(this.id,
125125
id, argv, Int32(argc),
126126
&payload1, &payload2
127127
)

Diff for: Sources/JavaScriptKit/FundamentalObjects/JSObject.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ public class JSObject: Equatable {
130130
/// - Parameter constructor: The constructor function to check.
131131
/// - Returns: The result of `instanceof` in the JavaScript environment.
132132
public func isInstanceOf(_ constructor: JSFunction) -> Bool {
133-
_instanceof(id, constructor.id)
133+
swjs_instanceof(id, constructor.id)
134134
}
135135

136136
static let _JS_Predef_Value_Global: JavaScriptObjectRef = 0
@@ -139,7 +139,7 @@ public class JSObject: Equatable {
139139
/// This allows access to the global properties and global names by accessing the `JSObject` returned.
140140
public static let global = JSObject(id: _JS_Predef_Value_Global)
141141

142-
deinit { _release(id) }
142+
deinit { swjs_release(id) }
143143

144144
/// Returns a Boolean value indicating whether two values point to same objects.
145145
///

Diff for: Sources/JavaScriptKit/FundamentalObjects/JSString.swift

+5-5
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,20 @@ public struct JSString: LosslessStringConvertible, Equatable {
2323
lazy var jsRef: JavaScriptObjectRef = {
2424
self.shouldDealocateRef = true
2525
return buffer.withUTF8 { bufferPtr in
26-
return _decode_string(bufferPtr.baseAddress!, Int32(bufferPtr.count))
26+
return swjs_decode_string(bufferPtr.baseAddress!, Int32(bufferPtr.count))
2727
}
2828
}()
2929

3030
lazy var buffer: String = {
3131
var bytesRef: JavaScriptObjectRef = 0
32-
let bytesLength = Int(_encode_string(jsRef, &bytesRef))
32+
let bytesLength = Int(swjs_encode_string(jsRef, &bytesRef))
3333
// +1 for null terminator
3434
let buffer = malloc(Int(bytesLength + 1))!.assumingMemoryBound(to: UInt8.self)
3535
defer {
3636
free(buffer)
37-
_release(bytesRef)
37+
swjs_release(bytesRef)
3838
}
39-
_load_string(bytesRef, buffer)
39+
swjs_load_string(bytesRef, buffer)
4040
buffer[bytesLength] = 0
4141
return String(decodingCString: UnsafePointer(buffer), as: UTF8.self)
4242
}()
@@ -52,7 +52,7 @@ public struct JSString: LosslessStringConvertible, Equatable {
5252

5353
deinit {
5454
guard shouldDealocateRef else { return }
55-
_release(jsRef)
55+
swjs_release(jsRef)
5656
}
5757
}
5858

Diff for: Sources/JavaScriptKit/FundamentalObjects/JSThrowingFunction.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public class JSThrowingFunction {
4444
var exceptionKind = JavaScriptValueKindAndFlags()
4545
var exceptionPayload1 = JavaScriptPayload1()
4646
var exceptionPayload2 = JavaScriptPayload2()
47-
let resultObj = _call_throwing_new(
47+
let resultObj = swjs_call_throwing_new(
4848
self.base.id, argv, Int32(argc),
4949
&exceptionKind, &exceptionPayload1, &exceptionPayload2
5050
)
@@ -73,13 +73,13 @@ private func invokeJSFunction(_ jsFunc: JSFunction, arguments: [ConvertibleToJSV
7373
var payload1 = JavaScriptPayload1()
7474
var payload2 = JavaScriptPayload2()
7575
if let thisId = this?.id {
76-
let resultBitPattern = _call_function_with_this(
76+
let resultBitPattern = swjs_call_function_with_this(
7777
thisId, id, argv, Int32(argc),
7878
&payload1, &payload2
7979
)
8080
kindAndFlags = unsafeBitCast(resultBitPattern, to: JavaScriptValueKindAndFlags.self)
8181
} else {
82-
let resultBitPattern = _call_function(
82+
let resultBitPattern = swjs_call_function(
8383
id, argv, Int32(argc),
8484
&payload1, &payload2
8585
)

Diff for: Sources/JavaScriptKit/JSValue.swift

+6-6
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ extension JSValue: ExpressibleByNilLiteral {
196196

197197
public func getJSValue(this: JSObject, name: JSString) -> JSValue {
198198
var rawValue = RawJSValue()
199-
let rawBitPattern = _get_prop(
199+
let rawBitPattern = swjs_get_prop(
200200
this.id, name.asInternalJSRef(),
201201
&rawValue.payload1, &rawValue.payload2
202202
)
@@ -206,13 +206,13 @@ public func getJSValue(this: JSObject, name: JSString) -> JSValue {
206206

207207
public func setJSValue(this: JSObject, name: JSString, value: JSValue) {
208208
value.withRawJSValue { rawValue in
209-
_set_prop(this.id, name.asInternalJSRef(), rawValue.kind, rawValue.payload1, rawValue.payload2)
209+
swjs_set_prop(this.id, name.asInternalJSRef(), rawValue.kind, rawValue.payload1, rawValue.payload2)
210210
}
211211
}
212212

213213
public func getJSValue(this: JSObject, index: Int32) -> JSValue {
214214
var rawValue = RawJSValue()
215-
let rawBitPattern = _get_subscript(
215+
let rawBitPattern = swjs_get_subscript(
216216
this.id, index,
217217
&rawValue.payload1, &rawValue.payload2
218218
)
@@ -222,15 +222,15 @@ public func getJSValue(this: JSObject, index: Int32) -> JSValue {
222222

223223
public func setJSValue(this: JSObject, index: Int32, value: JSValue) {
224224
value.withRawJSValue { rawValue in
225-
_set_subscript(this.id, index,
225+
swjs_set_subscript(this.id, index,
226226
rawValue.kind,
227227
rawValue.payload1, rawValue.payload2)
228228
}
229229
}
230230

231231
public func getJSValue(this: JSObject, symbol: JSSymbol) -> JSValue {
232232
var rawValue = RawJSValue()
233-
let rawBitPattern = _get_prop(
233+
let rawBitPattern = swjs_get_prop(
234234
this.id, symbol.id,
235235
&rawValue.payload1, &rawValue.payload2
236236
)
@@ -240,7 +240,7 @@ public func getJSValue(this: JSObject, symbol: JSSymbol) -> JSValue {
240240

241241
public func setJSValue(this: JSObject, symbol: JSSymbol, value: JSValue) {
242242
value.withRawJSValue { rawValue in
243-
_set_prop(this.id, symbol.id, rawValue.kind, rawValue.payload1, rawValue.payload2)
243+
swjs_set_prop(this.id, symbol.id, rawValue.kind, rawValue.payload1, rawValue.payload2)
244244
}
245245
}
246246

Diff for: Sources/JavaScriptKit/XcodeSupport.swift

-102
This file was deleted.

0 commit comments

Comments
 (0)