Skip to content

Add support for Symbol objects via JSSymbol #179

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
24 changes: 24 additions & 0 deletions IntegrationTests/TestSuites/Sources/PrimaryTests/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -804,4 +804,28 @@ try test("Hashable Conformance") {
try expectEqual(firstHash, secondHash)
}

try test("Symbols") {
let symbol1 = JSSymbol("abc")
let symbol2 = JSSymbol("abc")
try expectNotEqual(symbol1, symbol2)
try expectEqual(symbol1.name, symbol2.name)
try expectEqual(symbol1.name, "abc")

try expectEqual(JSSymbol.iterator, JSSymbol.iterator)

// let hasInstanceClass = {
// prop: Object.assign(function () {}, {
// [Symbol.hasInstance]() { return true }
// })
// }.prop
let hasInstanceObject = JSObject.global.Object.function!.new()
hasInstanceObject.prop = JSClosure { _ in .undefined }.jsValue()
let hasInstanceClass = hasInstanceObject.prop.function!
hasInstanceClass[JSSymbol.hasInstance] = JSClosure { _ in
return .boolean(true)
}.jsValue()
try expectEqual(hasInstanceClass[JSSymbol.hasInstance].function!().boolean, true)
try expectEqual(JSObject.global.Object.isInstanceOf(hasInstanceClass), true)
}

Expectation.wait(expectations)
6 changes: 6 additions & 0 deletions Runtime/src/js-value.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export enum Kind {
Null = 4,
Undefined = 5,
Function = 6,
Symbol = 7,
}

export const decode = (
Expand Down Expand Up @@ -102,6 +103,11 @@ export const write = (
memory.writeUint32(payload1_ptr, memory.retain(value));
break;
}
case "symbol": {
memory.writeUint32(kind_ptr, exceptionBit | Kind.Symbol);
memory.writeUint32(payload1_ptr, memory.retain(value));
break;
}
default:
throw new Error(`Type "${typeof value}" is not supported yet`);
}
Expand Down
24 changes: 8 additions & 16 deletions Runtime/src/object-heap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,33 +22,25 @@ export class SwiftRuntimeHeap {
}

retain(value: any) {
const isObject = typeof value == "object";
const entry = this._heapEntryByValue.get(value);
if (isObject && entry) {
if (entry) {
entry.rc++;
return entry.id;
}
const id = this._heapNextKey++;
this._heapValueById.set(id, value);
if (isObject) {
this._heapEntryByValue.set(value, { id: id, rc: 1 });
}
this._heapEntryByValue.set(value, { id: id, rc: 1 });
return id;
}

release(ref: ref) {
const value = this._heapValueById.get(ref);
const isObject = typeof value == "object";
if (isObject) {
const entry = this._heapEntryByValue.get(value)!;
entry.rc--;
if (entry.rc != 0) return;

this._heapEntryByValue.delete(value);
this._heapValueById.delete(ref);
} else {
this._heapValueById.delete(ref);
}
const entry = this._heapEntryByValue.get(value)!;
entry.rc--;
if (entry.rc != 0) return;

this._heapEntryByValue.delete(value);
this._heapValueById.delete(ref);
}

referenceHeap(ref: ref) {
Expand Down
5 changes: 5 additions & 0 deletions Sources/JavaScriptKit/ConvertibleToJSValue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,8 @@ extension RawJSValue: ConvertibleToJSValue {
return .undefined
case .function:
return .function(JSFunction(id: UInt32(payload1)))
case .symbol:
return .symbol(JSSymbol(id: UInt32(payload1)))
}
}
}
Expand Down Expand Up @@ -225,6 +227,9 @@ extension JSValue {
case let .function(functionRef):
kind = .function
payload1 = JavaScriptPayload1(functionRef.id)
case let .symbol(symbolRef):
kind = .symbol
payload1 = JavaScriptPayload1(symbolRef.id)
}
let rawValue = RawJSValue(kind: kind, payload1: payload1, payload2: payload2)
return body(rawValue)
Expand Down
17 changes: 8 additions & 9 deletions Sources/JavaScriptKit/FundamentalObjects/JSFunction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,14 @@ import _CJavaScriptKit
/// ```
///
public class JSFunction: JSObject {

/// Call this function with given `arguments` and binding given `this` as context.
/// - Parameters:
/// - this: The value to be passed as the `this` parameter to this function.
/// - arguments: Arguments to be passed to this function.
/// - Returns: The result of this call.
@discardableResult
public func callAsFunction(this: JSObject? = nil, arguments: [ConvertibleToJSValue]) -> JSValue {
invokeNonThrowingJSFunction(self, arguments: arguments, this: this)
invokeNonThrowingJSFunction(self, arguments: arguments, this: this).jsValue()
}

/// A variadic arguments version of `callAsFunction`.
Expand All @@ -41,7 +40,7 @@ public class JSFunction: JSObject {
public func new(arguments: [ConvertibleToJSValue]) -> JSObject {
arguments.withRawJSValues { rawValues in
rawValues.withUnsafeBufferPointer { bufferPointer in
return JSObject(id: _call_new(self.id, bufferPointer.baseAddress!, Int32(bufferPointer.count)))
JSObject(id: _call_new(self.id, bufferPointer.baseAddress!, Int32(bufferPointer.count)))
}
}
}
Expand Down Expand Up @@ -75,7 +74,7 @@ public class JSFunction: JSObject {
fatalError("unavailable")
}

public override class func construct(from value: JSValue) -> Self? {
override public class func construct(from value: JSValue) -> Self? {
return value.function as? Self
}

Expand All @@ -84,18 +83,18 @@ public class JSFunction: JSObject {
}
}

private func invokeNonThrowingJSFunction(_ jsFunc: JSFunction, arguments: [ConvertibleToJSValue], this: JSObject?) -> JSValue {
func invokeNonThrowingJSFunction(_ jsFunc: JSFunction, arguments: [ConvertibleToJSValue], this: JSObject?) -> RawJSValue {
arguments.withRawJSValues { rawValues in
rawValues.withUnsafeBufferPointer { bufferPointer -> (JSValue) 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)
jsFunc.id, argv, Int32(argc),
&kindAndFlags, &payload1, &payload2)
} else {
_call_function_no_catch(
jsFunc.id, argv, Int32(argc),
Expand All @@ -104,7 +103,7 @@ private func invokeNonThrowingJSFunction(_ jsFunc: JSFunction, arguments: [Conve
}
assert(!kindAndFlags.isException)
let result = RawJSValue(kind: kindAndFlags.kind, payload1: payload1, payload2: payload2)
return result.jsValue()
return result
}
}
}
8 changes: 8 additions & 0 deletions Sources/JavaScriptKit/FundamentalObjects/JSObject.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,14 @@ public class JSObject: Equatable {
set { setJSValue(this: self, index: Int32(index), value: newValue) }
}

/// Access the `symbol` member dynamically through JavaScript and Swift runtime bridge library.
/// - Parameter symbol: The name of this object's member to access.
/// - Returns: The value of the `name` member of this object.
public subscript(_ name: JSSymbol) -> JSValue {
get { getJSValue(this: self, symbol: name) }
set { setJSValue(this: self, symbol: name, value: newValue) }
}

/// A modifier to call methods as throwing methods capturing `this`
///
///
Expand Down
56 changes: 56 additions & 0 deletions Sources/JavaScriptKit/FundamentalObjects/JSSymbol.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import _CJavaScriptKit

private let Symbol = JSObject.global.Symbol.function!

public class JSSymbol: JSObject {
public var name: String? { self["description"].string }

public init(_ description: JSString) {
// can’t do `self =` so we have to get the ID manually
let result = invokeNonThrowingJSFunction(Symbol, arguments: [description], this: nil)
precondition(result.kind == .symbol)
super.init(id: UInt32(result.payload1))
}
@_disfavoredOverload
public convenience init(_ description: String) {
self.init(JSString(description))
}

override init(id: JavaScriptObjectRef) {
super.init(id: id)
}

public static func `for`(key: JSString) -> JSSymbol {
Symbol.for!(key).symbol!
}

@_disfavoredOverload
public static func `for`(key: String) -> JSSymbol {
Symbol.for!(key).symbol!
}

public static func key(for symbol: JSSymbol) -> JSString? {
Symbol.keyFor!(symbol).jsString
}

@_disfavoredOverload
public static func key(for symbol: JSSymbol) -> String? {
Symbol.keyFor!(symbol).string
}
}

extension JSSymbol {
public static let asyncIterator: JSSymbol! = Symbol.asyncIterator.symbol
public static let hasInstance: JSSymbol! = Symbol.hasInstance.symbol
public static let isConcatSpreadable: JSSymbol! = Symbol.isConcatSpreadable.symbol
public static let iterator: JSSymbol! = Symbol.iterator.symbol
public static let match: JSSymbol! = Symbol.match.symbol
public static let matchAll: JSSymbol! = Symbol.matchAll.symbol
public static let replace: JSSymbol! = Symbol.replace.symbol
public static let search: JSSymbol! = Symbol.search.symbol
public static let species: JSSymbol! = Symbol.species.symbol
public static let split: JSSymbol! = Symbol.split.symbol
public static let toPrimitive: JSSymbol! = Symbol.toPrimitive.symbol
public static let toStringTag: JSSymbol! = Symbol.toStringTag.symbol
public static let unscopables: JSSymbol! = Symbol.unscopables.symbol
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't these be autogenerated from IDL in DOMKit?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately not, as the ECMAScript spec (where these are defined) does not use IDL. (And, technically, it isn’t part of the DOM — it’s a language feature available in Node/Deno/plain JavaScriptCore/etc)

}
66 changes: 44 additions & 22 deletions Sources/JavaScriptKit/JSValue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public enum JSValue: Equatable {
case null
case undefined
case function(JSFunction)
case symbol(JSSymbol)

/// Returns the `Bool` value of this JS value if its type is boolean.
/// If not, returns `nil`.
Expand Down Expand Up @@ -67,6 +68,13 @@ public enum JSValue: Equatable {
}
}

public var symbol: JSSymbol? {
switch self {
case let .symbol(symbol): return symbol
default: return nil
}
}

/// Returns the `true` if this JS value is null.
/// If not, returns `false`.
public var isNull: Bool {
Expand All @@ -80,39 +88,38 @@ public enum JSValue: Equatable {
}
}

extension JSValue {
public extension JSValue {
/// An unsafe convenience method of `JSObject.subscript(_ name: String) -> ((ConvertibleToJSValue...) -> JSValue)?`
/// - Precondition: `self` must be a JavaScript Object and specified member should be a callable object.
public subscript(dynamicMember name: String) -> ((ConvertibleToJSValue...) -> JSValue) {
subscript(dynamicMember name: String) -> ((ConvertibleToJSValue...) -> JSValue) {
object![dynamicMember: name]!
}

/// An unsafe convenience method of `JSObject.subscript(_ index: Int) -> JSValue`
/// - Precondition: `self` must be a JavaScript Object.
public subscript(dynamicMember name: String) -> JSValue {
subscript(dynamicMember name: String) -> JSValue {
get { self.object![name] }
set { self.object![name] = newValue }
}

/// An unsafe convenience method of `JSObject.subscript(_ index: Int) -> JSValue`
/// - Precondition: `self` must be a JavaScript Object.
public subscript(_ index: Int) -> JSValue {
subscript(_ index: Int) -> JSValue {
get { object![index] }
set { object![index] = newValue }
}
}

extension JSValue: Swift.Error {}

extension JSValue {
public func fromJSValue<Type>() -> Type? where Type: ConstructibleFromJSValue {
public extension JSValue {
func fromJSValue<Type>() -> Type? where Type: ConstructibleFromJSValue {
return Type.construct(from: self)
}
}

extension JSValue {

public static func string(_ value: String) -> JSValue {
public extension JSValue {
static func string(_ value: String) -> JSValue {
.string(JSString(value))
}

Expand Down Expand Up @@ -141,12 +148,12 @@ extension JSValue {
/// eventListenter.release()
/// ```
@available(*, deprecated, message: "Please create JSClosure directly and manage its lifetime manually.")
public static func function(_ body: @escaping ([JSValue]) -> JSValue) -> JSValue {
static func function(_ body: @escaping ([JSValue]) -> JSValue) -> JSValue {
.object(JSClosure(body))
}

@available(*, deprecated, renamed: "object", message: "JSClosure is no longer a subclass of JSFunction. Use .object(closure) instead.")
public static func function(_ closure: JSClosure) -> JSValue {
static func function(_ closure: JSClosure) -> JSValue {
.object(closure)
}
}
Expand All @@ -170,7 +177,7 @@ extension JSValue: ExpressibleByFloatLiteral {
}

extension JSValue: ExpressibleByNilLiteral {
public init(nilLiteral: ()) {
public init(nilLiteral _: ()) {
self = .null
}
}
Expand Down Expand Up @@ -205,14 +212,28 @@ public func setJSValue(this: JSObject, index: Int32, value: JSValue) {
}
}

extension JSValue {
/// Return `true` if this value is an instance of the passed `constructor` function.
/// Returns `false` for everything except objects and functions.
/// - Parameter constructor: The constructor function to check.
/// - Returns: The result of `instanceof` in the JavaScript environment.
public func isInstanceOf(_ constructor: JSFunction) -> Bool {
public func getJSValue(this: JSObject, symbol: JSSymbol) -> JSValue {
var rawValue = RawJSValue()
_get_prop(this.id, symbol.id,
&rawValue.kind,
&rawValue.payload1, &rawValue.payload2)
return rawValue.jsValue()
}

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

public extension JSValue {
/// Return `true` if this value is an instance of the passed `constructor` function.
/// Returns `false` for everything except objects and functions.
/// - Parameter constructor: The constructor function to check.
/// - Returns: The result of `instanceof` in the JavaScript environment.
func isInstanceOf(_ constructor: JSFunction) -> Bool {
switch self {
case .boolean, .string, .number, .null, .undefined:
case .boolean, .string, .number, .null, .undefined, .symbol:
return false
case let .object(ref):
return ref.isInstanceOf(constructor)
Expand All @@ -227,11 +248,12 @@ extension JSValue: CustomStringConvertible {
switch self {
case let .boolean(boolean):
return boolean.description
case .string(let string):
case let .string(string):
return string.description
case .number(let number):
case let .number(number):
return number.description
case .object(let object), .function(let object as JSObject):
case let .object(object), let .function(object as JSObject),
.symbol(let object as JSObject):
return object.toString!().fromJSValue()!
case .null:
return "null"
Expand Down
Loading