Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
Switch back to JSFunctionRef.new()
  • Loading branch information
j-f1 committed Aug 4, 2020
commit 009d45172b48b4258174c517e230c1c72e845ecb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ let objectHeap = Benchmark("Object heap")

let global = JSObjectRef.global
let Object = global.Object.function!
global.objectHeapDummy = .object(Object(.new))
global.objectHeapDummy = .object(Object.new())
objectHeap.testSuite("Increment and decrement RC") {
for _ in 0 ..< 100 {
_ = global.objectHeapDummy
Expand Down
6 changes: 3 additions & 3 deletions IntegrationTests/TestSuites/Sources/PrimaryTests/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -225,15 +225,15 @@ New_Object_Construction: do {
// }
// ```
let objectConstructor = try expectFunction(getJSValue(this: .global, name: "Animal"))
let cat1 = objectConstructor(new: "Tama", 3, true)
let cat1 = objectConstructor.new("Tama", 3, true)
try expectEqual(getJSValue(this: cat1, name: "name"), .string("Tama"))
try expectEqual(getJSValue(this: cat1, name: "age"), .number(3))
try expectEqual(JSObjectRef.instanceof(cat1, constructor: objectConstructor), true)
try expectEqual(JSObjectRef.instanceof(cat1, constructor: try expectFunction(getJSValue(this: .global, name: "Array"))), false)
let cat1Bark = try expectFunction(getJSValue(this: cat1, name: "bark"))
try expectEqual(cat1Bark(), .string("nyan"))

let dog1 = objectConstructor(new: "Pochi", 3, false)
let dog1 = objectConstructor.new("Pochi", 3, false)
let dog1Bark = try expectFunction(getJSValue(this: dog1, name: "bark"))
try expectEqual(dog1Bark(), .string("wan"))
} catch {
Expand All @@ -255,7 +255,7 @@ Call_Function_With_This: do {
// }
// ```
let objectConstructor = try expectFunction(getJSValue(this: .global, name: "Animal"))
let cat1 = objectConstructor(new: "Tama", 3, true)
let cat1 = objectConstructor.new("Tama", 3, true)
let getIsCat = try expectFunction(getJSValue(this: cat1, name: "getIsCat"))

// Direct call without this
Expand Down
14 changes: 7 additions & 7 deletions Sources/JavaScriptKit/JSFunction.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import _CJavaScriptKit

public enum _JSFunctionConstructorSymbol {
case new
}

public class JSFunctionRef: JSObjectRef {
@discardableResult
func callAsFunction(this: JSObjectRef? = nil, args: [JSValueConvertible]) -> JSValue {
Comment thread
j-f1 marked this conversation as resolved.
Outdated
Expand Down Expand Up @@ -33,11 +29,15 @@ public class JSFunctionRef: JSObjectRef {
self(this: this, args: args)
}

public func callAsFunction(new args: JSValueConvertible...) -> JSObjectRef {
self(.new, args: args)
public func new(_ args: JSValueConvertible...) -> JSObjectRef {
self.new(args: args)
}

public func callAsFunction(_: _JSFunctionConstructorSymbol, args: [JSValueConvertible] = []) -> JSObjectRef {
// Guaranteed to return an object because either:
// a) the constructor explicitly returns an object, or
// b) the constructor returns nothing, which causes JS to return the `this` value, or
// c) the constructor returns undefined, null or a non-object, in which case JS also returns `this`.
public func new(args: [JSValueConvertible]) -> JSObjectRef {
Comment thread
j-f1 marked this conversation as resolved.
Outdated
args.withRawJSValues { rawValues in
rawValues.withUnsafeBufferPointer { bufferPointer in
let argv = bufferPointer.baseAddress
Expand Down
4 changes: 2 additions & 2 deletions Sources/JavaScriptKit/JSValueConvertible.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ extension Dictionary where Value: JSValueConvertible, Key == String {

extension Dictionary: JSValueConvertible where Value == JSValueConvertible, Key == String {
public subscript(jsValue _: ()) -> JSValue {
let object = Object(.new)
let object = Object.new()
for (key, value) in self {
object[key] = JSValue(from: value)
}
Expand All @@ -88,7 +88,7 @@ extension Array where Element: JSValueConvertible {

extension Array: JSValueConvertible where Element == JSValueConvertible {
public subscript(jsValue _: ()) -> JSValue {
let array = Array(new: count)
let array = Array.new(count)
for (index, element) in enumerated() {
array[index] = JSValue(from: element)
}
Expand Down