Skip to content

Commit bb9e31c

Browse files
committed
Updated from kateinoigakukun/JavaScriptKit
2 parents 830a969 + 85b8617 commit bb9e31c

File tree

8 files changed

+44
-24
lines changed

8 files changed

+44
-24
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ jobs:
88
os: [macOS-10.15]
99
include:
1010
- os: macOS-10.15
11-
toolchain: https://github.com/swiftwasm/swift/releases/download/swift-wasm-DEVELOPMENT-SNAPSHOT-2020-05-10-a/swift-wasm-DEVELOPMENT-SNAPSHOT-2020-05-10-a-osx.tar.gz
11+
toolchain: https://github.com/swiftwasm/swift/releases/download/swift-wasm-DEVELOPMENT-SNAPSHOT-2020-06-03-a/swift-wasm-DEVELOPMENT-SNAPSHOT-2020-06-03-a-osx.tar.gz
1212
runs-on: ${{ matrix.os }}
1313
steps:
1414
- name: Checkout

.swift-version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
wasm-DEVELOPMENT-SNAPSHOT-2020-05-10-a
1+
wasm-DEVELOPMENT-SNAPSHOT-2020-06-03-a

IntegrationTests/JavaScriptKitExec/Sources/JavaScriptKitExec/main.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ Host_Function_Registration: do {
184184
let prop_6Ref = try expectObject(prop_6)
185185

186186
var isHostFunc1Called = false
187-
let hostFunc1 = JSFunctionRef.from { (arguments) -> JSValue in
187+
let hostFunc1 = JSClosure { (arguments) -> JSValue in
188188
isHostFunc1Called = true
189189
return .number(1)
190190
}
@@ -196,7 +196,9 @@ Host_Function_Registration: do {
196196
try expectEqual(call_host_1Func(), .number(1))
197197
try expectEqual(isHostFunc1Called, true)
198198

199-
let hostFunc2 = JSFunctionRef.from { (arguments) -> JSValue in
199+
hostFunc1.release()
200+
201+
let hostFunc2 = JSClosure { (arguments) -> JSValue in
200202
do {
201203
let input = try expectNumber(arguments[0])
202204
return .number(input * 2)
@@ -207,7 +209,7 @@ Host_Function_Registration: do {
207209

208210
try expectEqual(hostFunc2(3), .number(6))
209211
_ = try expectString(hostFunc2(true))
210-
212+
hostFunc2.release()
211213
} catch {
212214
print(error)
213215
}

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ The toolchains can be installed via [`swiftenv`](https://github.com/kylef/swifte
1313
e.g.
1414
```sh
1515

16-
$ swiftenv install https://github.com/swiftwasm/swift/releases/download/swift-wasm-DEVELOPMENT-SNAPSHOT-2020-05-10-a/swift-wasm-DEVELOPMENT-SNAPSHOT-2020-05-10-a-osx.tar.gz
16+
$ swiftenv install https://github.com/swiftwasm/swift/releases/download/swift-wasm-DEVELOPMENT-SNAPSHOT-2020-06-03-a/swift-wasm-DEVELOPMENT-SNAPSHOT-2020-06-03-a-osx.tar.gz
1717
$ swift --version
1818
Swift version 5.3-dev (LLVM 47c28180d7, Swift 5f96d487e0)
1919
Target: x86_64-apple-darwin19.3.0
@@ -65,7 +65,7 @@ struct Pet: Codable {
6565
}
6666

6767
let jsPet = JSObjectRef.global.pet
68-
let swiftPet: Pet = JSValueDecoder().decode(from: jsPet)
68+
let swiftPet: Pet = try JSValueDecoder().decode(from: jsPet)
6969

7070
alert("Swift is running on browser!")
7171
```

Sources/JavaScriptKit/JS Types/JSPromise.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public final class JSPromise<Success>: JSType where Success: JSValueConvertible,
2929
- parameter executor: A function to be executed by the Promise. The executor is custom code that ties an outcome to a promise. You, the programmer, write the executor.
3030
*/
3131
public init(executor block: @escaping ((Success) -> (), (JSError) -> ()) -> ()) {
32-
let executor = JSFunctionRef.from { (arguments) in
32+
let executor = JSClosure { (arguments) in
3333
let resolutionFunc = arguments[0].function
3434
let rejectionFunc = arguments[1].function
3535
block({ resolutionFunc?($0.jsValue()) }, { rejectionFunc?($0.jsValue()) })
@@ -96,7 +96,7 @@ public final class JSPromise<Success>: JSType where Success: JSValueConvertible,
9696
guard let function = jsObject.then.function
9797
else { fatalError("Invalid function \(#function)") }
9898

99-
let success = JSFunctionRef.from { (arguments) in
99+
let success = JSClosure { (arguments) in
100100
if let value = arguments.first.flatMap({ Success.construct(from: $0) }) {
101101
return onFulfilled(value)
102102
} else {
@@ -105,7 +105,7 @@ public final class JSPromise<Success>: JSType where Success: JSValueConvertible,
105105
}
106106
}
107107

108-
let errorFunction = JSFunctionRef.from { (arguments) in
108+
let errorFunction = JSClosure { (arguments) in
109109
if let value = arguments.first.flatMap({ JSError.construct(from: $0) }) {
110110
onRejected(value)
111111
} else {
@@ -123,7 +123,7 @@ public final class JSPromise<Success>: JSType where Success: JSValueConvertible,
123123
guard let function = jsObject.then.function
124124
else { fatalError("Invalid function \(#function)") }
125125

126-
let success = JSFunctionRef.from { (arguments) in
126+
let success = JSClosure { (arguments) in
127127
if let value = arguments.first.flatMap({ Success.construct(from: $0) }) {
128128
return onFulfilled(value)
129129
} else {
@@ -248,7 +248,7 @@ public final class JSPromise<Success>: JSType where Success: JSValueConvertible,
248248
public func `catch`(_ completion: @escaping (JSError) -> ()) {
249249
guard let function = jsObject.catch.function
250250
else { fatalError("Invalid function \(#function)") }
251-
let errorFunction = JSFunctionRef.from { (arguments) in
251+
let errorFunction = JSClosure { (arguments) in
252252
if let value = arguments.first.flatMap({ JSError.construct(from: $0) }) {
253253
completion(value)
254254
} else {

Sources/JavaScriptKit/JSFunctionRef.swift

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,10 @@ public class JSFunctionRef: JSObjectRef {
5656
}
5757
}
5858
}
59-
60-
static var sharedFunctions: [([JSValue]) -> JSValue] = []
6159

60+
@available(*, unavailable, message: "Please use JSClosure instead")
6261
public static func from(_ body: @escaping ([JSValue]) -> JSValue) -> JSFunctionRef {
63-
let id = JavaScriptHostFuncRef(sharedFunctions.count)
64-
sharedFunctions.append(body)
65-
var funcRef: JavaScriptObjectRef = 0
66-
_create_function(id, &funcRef)
67-
68-
return JSFunctionRef(id: funcRef)
62+
fatalError("unavailable")
6963
}
7064

7165
// MARK: - JSValueConvertible
@@ -75,7 +69,29 @@ public class JSFunctionRef: JSObjectRef {
7569
}
7670
}
7771

78-
// MARK: - C Functions
72+
public class JSClosure: JSFunctionRef {
73+
74+
static var sharedFunctions: [JavaScriptHostFuncRef: ([JSValue]) -> JSValue] = [:]
75+
76+
private var hostFuncRef: JavaScriptHostFuncRef = 0
77+
78+
public init(_ body: @escaping ([JSValue]) -> JSValue) {
79+
super.init(id: 0)
80+
let objectId = ObjectIdentifier(self)
81+
let funcRef = JavaScriptHostFuncRef(bitPattern: Int32(objectId.hashValue))
82+
Self.sharedFunctions[funcRef] = body
83+
84+
var objectRef: JavaScriptObjectRef = 0
85+
_create_function(funcRef, &objectRef)
86+
87+
self.hostFuncRef = funcRef
88+
self.id = objectRef
89+
}
90+
91+
public func release() {
92+
Self.sharedFunctions[hostFuncRef] = nil
93+
}
94+
}
7995

8096
@_cdecl("swjs_prepare_host_function_call")
8197
internal func _prepare_host_function_call(_ argc: Int32) -> UnsafeMutableRawPointer {
@@ -93,7 +109,9 @@ internal func _call_host_function(
93109
_ hostFuncRef: JavaScriptHostFuncRef,
94110
_ argv: UnsafePointer<RawJSValue>, _ argc: Int32,
95111
_ callbackFuncRef: JavaScriptObjectRef) {
96-
let hostFunc = JSFunctionRef.sharedFunctions[Int(hostFuncRef)]
112+
guard let hostFunc = JSClosure.sharedFunctions[hostFuncRef] else {
113+
fatalError("The function was already released")
114+
}
97115
let args = UnsafeBufferPointer(start: argv, count: Int(argc)).map {
98116
$0.jsValue()
99117
}

Sources/JavaScriptKit/JSObjectRef.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public class JSObjectRef {
77

88
// MARK: - Properties
99

10-
public let id: ID
10+
public internal(set) var id: ID
1111

1212
// MARK: - Initialization
1313

Sources/JavaScriptKit/JSValue.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public extension JSValue {
5656

5757
extension JSValue {
5858
public static func function(_ body: @escaping ([JSValue]) -> JSValue) -> JSValue {
59-
.function(JSFunctionRef.from(body))
59+
.function(JSClosure(body))
6060
}
6161
}
6262

0 commit comments

Comments
 (0)