Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
b9fb002
Added strongly typed classes
colemancda Jun 3, 2020
ff13814
Added `JSPromise`
colemancda Jun 3, 2020
94c7dbb
Working on `JSPromise`
colemancda Jun 3, 2020
4714de8
Working on `JSPromise`
colemancda Jun 3, 2020
b5c8fa3
Added `JSError`
colemancda Jun 4, 2020
1a44d94
Updated `JSType`
colemancda Jun 4, 2020
c88b849
Working on `JSBluetooth`
colemancda Jun 4, 2020
128a862
Working on `JSBluetooth`
colemancda Jun 4, 2020
2f0eae8
Updated example
colemancda Jun 4, 2020
b75da55
Updated `JSBluetoothDevice`
colemancda Jun 4, 2020
3f47ae9
Added `JSConsole`
colemancda Jun 4, 2020
cde8d76
Added `JSBluetoothRemoteGATTServer`
colemancda Jun 4, 2020
4fb0f75
Updated `JSPromise`
colemancda Jun 4, 2020
66dd688
Updated logging
colemancda Jun 4, 2020
e14b6ac
Updated `JSPromise`
colemancda Jun 4, 2020
e4f4025
Updated `JSConsole`
colemancda Jun 4, 2020
ea55aa5
Fixed `JSType.toString()`
colemancda Jun 4, 2020
e002eb8
Updated `JSPromise`
colemancda Jun 4, 2020
195b5e2
Updated logging
colemancda Jun 4, 2020
f1be2de
Added `JSBluetooth.isAvailable`
colemancda Jun 4, 2020
7eb4587
Updated `JSPromise`
colemancda Jun 4, 2020
b1a06cb
Added `JSBluetoothRemoteGATTService`
colemancda Jun 4, 2020
235a4fe
Updated `JSPromise`
colemancda Jun 4, 2020
31e87ef
Renamed `JSDecoder`
colemancda Jun 4, 2020
8d5a12b
Fixed `JSConsole.assert()`
colemancda Jun 4, 2020
23d930b
Added `JSBluetooth.devices`
colemancda Jun 5, 2020
4356521
Added `JSEncoder`
colemancda Jun 5, 2020
4ed88b3
Added `JSObject.keys`
colemancda Jun 5, 2020
fdef8c2
Updated dependencies
colemancda Jun 5, 2020
a3093b9
Added `JSDate`
colemancda Jun 5, 2020
cfc85ce
Updated assertions
colemancda Jun 5, 2020
9fbb5e7
Updated `JSDate`
colemancda Jun 5, 2020
8f05e88
Fixed `JSEncoder`
colemancda Jun 5, 2020
c976028
Fixed `CodingKey.sanitizedName`
colemancda Jun 5, 2020
6a1528f
Fixed `JSArray`
colemancda Jun 5, 2020
699a1f0
Fixed `JSEncoder`
colemancda Jun 5, 2020
830a969
Updated dependencies
colemancda Jun 5, 2020
bb9e31c
Updated from `kateinoigakukun/JavaScriptKit`
colemancda Jun 5, 2020
d56d684
Fixed `JSDate`
colemancda Jun 5, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Updated from kateinoigakukun/JavaScriptKit
  • Loading branch information
colemancda committed Jun 5, 2020
commit bb9e31c5884eb1338a7463fa5c99b69ec15b2c7b
10 changes: 5 additions & 5 deletions Sources/JavaScriptKit/JS Types/JSPromise.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public final class JSPromise<Success>: JSType where Success: JSValueConvertible,
- 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.
*/
public init(executor block: @escaping ((Success) -> (), (JSError) -> ()) -> ()) {
let executor = JSFunctionRef.from { (arguments) in
let executor = JSClosure { (arguments) in
let resolutionFunc = arguments[0].function
let rejectionFunc = arguments[1].function
block({ resolutionFunc?($0.jsValue()) }, { rejectionFunc?($0.jsValue()) })
Expand Down Expand Up @@ -96,7 +96,7 @@ public final class JSPromise<Success>: JSType where Success: JSValueConvertible,
guard let function = jsObject.then.function
else { fatalError("Invalid function \(#function)") }

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

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

let success = JSFunctionRef.from { (arguments) in
let success = JSClosure { (arguments) in
if let value = arguments.first.flatMap({ Success.construct(from: $0) }) {
return onFulfilled(value)
} else {
Expand Down Expand Up @@ -248,7 +248,7 @@ public final class JSPromise<Success>: JSType where Success: JSValueConvertible,
public func `catch`(_ completion: @escaping (JSError) -> ()) {
guard let function = jsObject.catch.function
else { fatalError("Invalid function \(#function)") }
let errorFunction = JSFunctionRef.from { (arguments) in
let errorFunction = JSClosure { (arguments) in
if let value = arguments.first.flatMap({ JSError.construct(from: $0) }) {
completion(value)
} else {
Expand Down
27 changes: 24 additions & 3 deletions Sources/JavaScriptKit/JSFunctionRef.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,8 @@ public class JSFunctionRef: JSObjectRef {
}
}
}

static var sharedFunctions: [([JSValue]) -> JSValue] = []

@available(*, unavailable, message: "Please use JSClosure instead")
public static func from(_ body: @escaping ([JSValue]) -> JSValue) -> JSFunctionRef {
fatalError("unavailable")
}
Expand All @@ -70,7 +69,29 @@ public class JSFunctionRef: JSObjectRef {
}
}

// MARK: - C Functions
public class JSClosure: JSFunctionRef {

static var sharedFunctions: [JavaScriptHostFuncRef: ([JSValue]) -> JSValue] = [:]

private var hostFuncRef: JavaScriptHostFuncRef = 0

public init(_ body: @escaping ([JSValue]) -> JSValue) {
super.init(id: 0)
let objectId = ObjectIdentifier(self)
let funcRef = JavaScriptHostFuncRef(bitPattern: Int32(objectId.hashValue))
Self.sharedFunctions[funcRef] = body

var objectRef: JavaScriptObjectRef = 0
_create_function(funcRef, &objectRef)

self.hostFuncRef = funcRef
self.id = objectRef
}

public func release() {
Self.sharedFunctions[hostFuncRef] = nil
}
}

@_cdecl("swjs_prepare_host_function_call")
internal func _prepare_host_function_call(_ argc: Int32) -> UnsafeMutableRawPointer {
Expand Down
2 changes: 1 addition & 1 deletion Sources/JavaScriptKit/JSObjectRef.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public class JSObjectRef {

// MARK: - Properties

public let id: ID
public internal(set) var id: ID

// MARK: - Initialization

Expand Down
You are viewing a condensed version of this merge commit. You can view the full changes here.