Skip to content

Commit

Permalink
Revert "Allocate function call argument buffer on stack"
Browse files Browse the repository at this point in the history
This reverts commit 8d17f5c.
  • Loading branch information
kateinoigakukun committed Aug 16, 2022
1 parent 8d17f5c commit d684978
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 79 deletions.
36 changes: 18 additions & 18 deletions Sources/JavaScriptKit/ConvertibleToJSValue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ extension RawJSValue: ConvertibleToJSValue {
}

extension JSValue {
func toRawJSValue() -> RawJSValue {
func withRawJSValue<T>(_ body: (RawJSValue) -> T) -> T {
let kind: JavaScriptValueKind
let payload1: JavaScriptPayload1
var payload2: JavaScriptPayload2 = 0
Expand All @@ -226,7 +226,7 @@ extension JSValue {
payload1 = 0
payload2 = numberValue
case let .string(string):
return string.toRawJSValue()
return string.withRawJSValue(body)
case let .object(ref):
kind = .object
payload1 = JavaScriptPayload1(ref.id)
Expand All @@ -246,30 +246,30 @@ extension JSValue {
kind = .bigInt
payload1 = JavaScriptPayload1(bigIntRef.id)
}
return RawJSValue(kind: kind, payload1: payload1, payload2: payload2)
let rawValue = RawJSValue(kind: kind, payload1: payload1, payload2: payload2)
return body(rawValue)
}
}

extension Array where Element == JSValue {
func withRawJSValues<T>(_ body: (UnsafeBufferPointer<RawJSValue>) -> T) -> T {
withUnsafeTemporaryAllocation(of: RawJSValue.self, capacity: self.count) { buffer in
for (index, value) in self.enumerated() {
buffer[index] = value.toRawJSValue()
extension Array where Element == ConvertibleToJSValue {
func withRawJSValues<T>(_ body: ([RawJSValue]) -> T) -> T {
func _withRawJSValues<T>(
_ values: [ConvertibleToJSValue], _ index: Int,
_ results: inout [RawJSValue], _ body: ([RawJSValue]) -> T
) -> T {
if index == values.count { return body(results) }
return values[index].jsValue.withRawJSValue { (rawValue) -> T in
results.append(rawValue)
return _withRawJSValues(values, index + 1, &results, body)
}
return body(UnsafeBufferPointer(buffer))
}
var _results = [RawJSValue]()
return _withRawJSValues(self, 0, &_results, body)
}
}

extension Array where Element == ConvertibleToJSValue {
func withRawJSValues<T>(_ body: (UnsafeBufferPointer<RawJSValue>) -> T) -> T {
map { $0.jsValue }.withRawJSValues(body)
}
}


extension Array where Element: ConvertibleToJSValue {
func withRawJSValues<T>(_ body: (UnsafeBufferPointer<RawJSValue>) -> T) -> T {
map { $0.jsValue }.withRawJSValues(body)
func withRawJSValues<T>(_ body: ([RawJSValue]) -> T) -> T {
[ConvertibleToJSValue].withRawJSValues(self)(body)
}
}
44 changes: 24 additions & 20 deletions Sources/JavaScriptKit/FundamentalObjects/JSFunction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,10 @@ public class JSFunction: JSObject {
/// - Parameter arguments: Arguments to be passed to this constructor function.
/// - Returns: A new instance of this constructor.
public func new(arguments: [ConvertibleToJSValue]) -> JSObject {
arguments.withRawJSValues { bufferPointer in
JSObject(id: _call_new(self.id, bufferPointer.baseAddress!, Int32(bufferPointer.count)))
arguments.withRawJSValues { rawValues in
rawValues.withUnsafeBufferPointer { bufferPointer in
JSObject(id: _call_new(self.id, bufferPointer.baseAddress!, Int32(bufferPointer.count)))
}
}
}

Expand Down Expand Up @@ -82,24 +84,26 @@ public class JSFunction: JSObject {
}

func invokeNonThrowingJSFunction(_ jsFunc: JSFunction, arguments: [ConvertibleToJSValue], this: JSObject?) -> RawJSValue {
arguments.withRawJSValues { bufferPointer in
let argv = bufferPointer.baseAddress
let argc = bufferPointer.count
var kindAndFlags = JavaScriptValueKindAndFlags()
var payload1 = JavaScriptPayload1()
var payload2 = JavaScriptPayload2()
if let thisId = this?.id {
_call_function_with_this_no_catch(thisId,
jsFunc.id, argv, Int32(argc),
&kindAndFlags, &payload1, &payload2)
} else {
_call_function_no_catch(
jsFunc.id, argv, Int32(argc),
&kindAndFlags, &payload1, &payload2
)
arguments.withRawJSValues { rawValues in
rawValues.withUnsafeBufferPointer { bufferPointer in
let argv = bufferPointer.baseAddress
let argc = bufferPointer.count
var kindAndFlags = JavaScriptValueKindAndFlags()
var payload1 = JavaScriptPayload1()
var payload2 = JavaScriptPayload2()
if let thisId = this?.id {
_call_function_with_this_no_catch(thisId,
jsFunc.id, argv, Int32(argc),
&kindAndFlags, &payload1, &payload2)
} else {
_call_function_no_catch(
jsFunc.id, argv, Int32(argc),
&kindAndFlags, &payload1, &payload2
)
}
assert(!kindAndFlags.isException)
let result = RawJSValue(kind: kindAndFlags.kind, payload1: payload1, payload2: payload2)
return result
}
assert(!kindAndFlags.isException)
let result = RawJSValue(kind: kindAndFlags.kind, payload1: payload1, payload2: payload2)
return result
}
}
5 changes: 3 additions & 2 deletions Sources/JavaScriptKit/FundamentalObjects/JSString.swift
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,10 @@ extension JSString {
guts.jsRef
}

func toRawJSValue() -> RawJSValue {
return RawJSValue(
func withRawJSValue<T>(_ body: (RawJSValue) -> T) -> T {
let rawValue = RawJSValue(
kind: .string, payload1: guts.jsRef, payload2: 0
)
return body(rawValue)
}
}
66 changes: 35 additions & 31 deletions Sources/JavaScriptKit/FundamentalObjects/JSThrowingFunction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,24 @@ public class JSThrowingFunction {
/// - Parameter arguments: Arguments to be passed to this constructor function.
/// - Returns: A new instance of this constructor.
public func new(arguments: [ConvertibleToJSValue]) throws -> JSObject {
try arguments.withRawJSValues { bufferPointer -> Result<JSObject, JSValue> in
let argv = bufferPointer.baseAddress
let argc = bufferPointer.count
try arguments.withRawJSValues { rawValues -> Result<JSObject, JSValue> in
rawValues.withUnsafeBufferPointer { bufferPointer in
let argv = bufferPointer.baseAddress
let argc = bufferPointer.count

var exceptionKind = JavaScriptValueKindAndFlags()
var exceptionPayload1 = JavaScriptPayload1()
var exceptionPayload2 = JavaScriptPayload2()
let resultObj = _call_throwing_new(
self.base.id, argv, Int32(argc),
&exceptionKind, &exceptionPayload1, &exceptionPayload2
)
if exceptionKind.isException {
let exception = RawJSValue(kind: exceptionKind.kind, payload1: exceptionPayload1, payload2: exceptionPayload2)
return .failure(exception.jsValue)
var exceptionKind = JavaScriptValueKindAndFlags()
var exceptionPayload1 = JavaScriptPayload1()
var exceptionPayload2 = JavaScriptPayload2()
let resultObj = _call_throwing_new(
self.base.id, argv, Int32(argc),
&exceptionKind, &exceptionPayload1, &exceptionPayload2
)
if exceptionKind.isException {
let exception = RawJSValue(kind: exceptionKind.kind, payload1: exceptionPayload1, payload2: exceptionPayload2)
return .failure(exception.jsValue)
}
return .success(JSObject(id: resultObj))
}
return .success(JSObject(id: resultObj))
}.get()
}

Expand All @@ -62,24 +64,26 @@ public class JSThrowingFunction {
}

private func invokeJSFunction(_ jsFunc: JSFunction, arguments: [ConvertibleToJSValue], this: JSObject?) throws -> JSValue {
let (result, isException): (JSValue, Bool) = arguments.withRawJSValues { bufferPointer in
let argv = bufferPointer.baseAddress
let argc = bufferPointer.count
var kindAndFlags = JavaScriptValueKindAndFlags()
var payload1 = JavaScriptPayload1()
var payload2 = JavaScriptPayload2()
if let thisId = this?.id {
_call_function_with_this(thisId,
jsFunc.id, argv, Int32(argc),
&kindAndFlags, &payload1, &payload2)
} else {
_call_function(
jsFunc.id, argv, Int32(argc),
&kindAndFlags, &payload1, &payload2
)
let (result, isException) = arguments.withRawJSValues { rawValues in
rawValues.withUnsafeBufferPointer { bufferPointer -> (JSValue, Bool) in
let argv = bufferPointer.baseAddress
let argc = bufferPointer.count
var kindAndFlags = JavaScriptValueKindAndFlags()
var payload1 = JavaScriptPayload1()
var payload2 = JavaScriptPayload2()
if let thisId = this?.id {
_call_function_with_this(thisId,
jsFunc.id, argv, Int32(argc),
&kindAndFlags, &payload1, &payload2)
} else {
_call_function(
jsFunc.id, argv, Int32(argc),
&kindAndFlags, &payload1, &payload2
)
}
let result = RawJSValue(kind: kindAndFlags.kind, payload1: payload1, payload2: payload2)
return (result.jsValue, kindAndFlags.isException)
}
let result = RawJSValue(kind: kindAndFlags.kind, payload1: payload1, payload2: payload2)
return (result.jsValue, kindAndFlags.isException)
}
if isException {
throw result
Expand Down
19 changes: 11 additions & 8 deletions Sources/JavaScriptKit/JSValue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,9 @@ public func getJSValue(this: JSObject, name: JSString) -> JSValue {
}

public func setJSValue(this: JSObject, name: JSString, value: JSValue) {
let rawValue = value.toRawJSValue()
_set_prop(this.id, name.asInternalJSRef(), rawValue.kind, rawValue.payload1, rawValue.payload2)
value.withRawJSValue { rawValue in
_set_prop(this.id, name.asInternalJSRef(), rawValue.kind, rawValue.payload1, rawValue.payload2)
}
}

public func getJSValue(this: JSObject, index: Int32) -> JSValue {
Expand All @@ -216,10 +217,11 @@ public func getJSValue(this: JSObject, index: Int32) -> JSValue {
}

public func setJSValue(this: JSObject, index: Int32, value: JSValue) {
let rawValue = value.toRawJSValue()
_set_subscript(this.id, index,
rawValue.kind,
rawValue.payload1, rawValue.payload2)
value.withRawJSValue { rawValue in
_set_subscript(this.id, index,
rawValue.kind,
rawValue.payload1, rawValue.payload2)
}
}

public func getJSValue(this: JSObject, symbol: JSSymbol) -> JSValue {
Expand All @@ -231,8 +233,9 @@ public func getJSValue(this: JSObject, symbol: JSSymbol) -> JSValue {
}

public func setJSValue(this: JSObject, symbol: JSSymbol, value: JSValue) {
let rawValue = value.toRawJSValue()
_set_prop(this.id, symbol.id, rawValue.kind, rawValue.payload1, rawValue.payload2)
value.withRawJSValue { rawValue in
_set_prop(this.id, symbol.id, rawValue.kind, rawValue.payload1, rawValue.payload2)
}
}

public extension JSValue {
Expand Down

0 comments on commit d684978

Please sign in to comment.