Skip to content

Commit 990479e

Browse files
Revert "Add support for Symbol objects via JSSymbol (#179)"
This reverts commit 081784b.
1 parent 95d0c4c commit 990479e

File tree

9 files changed

+47
-163
lines changed

9 files changed

+47
-163
lines changed

IntegrationTests/TestSuites/Sources/PrimaryTests/main.swift

-23
Original file line numberDiff line numberDiff line change
@@ -804,28 +804,5 @@ try test("Hashable Conformance") {
804804
try expectEqual(firstHash, secondHash)
805805
}
806806

807-
try test("Symbols") {
808-
let symbol1 = JSSymbol("abc")
809-
let symbol2 = JSSymbol("abc")
810-
try expectNotEqual(symbol1, symbol2)
811-
try expectEqual(symbol1.name, symbol2.name)
812-
try expectEqual(symbol1.name, "abc")
813-
814-
try expectEqual(JSSymbol.iterator, JSSymbol.iterator)
815-
816-
// let hasInstanceClass = {
817-
// prop: Object.assign(function () {}, {
818-
// [Symbol.hasInstance]() { return true }
819-
// })
820-
// }.prop
821-
let hasInstanceObject = JSObject.global.Object.function!.new()
822-
hasInstanceObject.prop = JSClosure { _ in .undefined }.jsValue
823-
let hasInstanceClass = hasInstanceObject.prop.function!
824-
hasInstanceClass[JSSymbol.hasInstance] = JSClosure { _ in
825-
return .boolean(true)
826-
}.jsValue
827-
try expectEqual(hasInstanceClass[JSSymbol.hasInstance].function!().boolean, true)
828-
try expectEqual(JSObject.global.Object.isInstanceOf(hasInstanceClass), true)
829-
}
830807

831808
Expectation.wait(expectations)

Runtime/src/js-value.ts

-6
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ export enum Kind {
1010
Null = 4,
1111
Undefined = 5,
1212
Function = 6,
13-
Symbol = 7,
1413
}
1514

1615
export const decode = (
@@ -103,11 +102,6 @@ export const write = (
103102
memory.writeUint32(payload1_ptr, memory.retain(value));
104103
break;
105104
}
106-
case "symbol": {
107-
memory.writeUint32(kind_ptr, exceptionBit | Kind.Symbol);
108-
memory.writeUint32(payload1_ptr, memory.retain(value));
109-
break;
110-
}
111105
default:
112106
throw new Error(`Type "${typeof value}" is not supported yet`);
113107
}

Runtime/src/object-heap.ts

+16-8
Original file line numberDiff line numberDiff line change
@@ -22,25 +22,33 @@ export class SwiftRuntimeHeap {
2222
}
2323

2424
retain(value: any) {
25+
const isObject = typeof value == "object";
2526
const entry = this._heapEntryByValue.get(value);
26-
if (entry) {
27+
if (isObject && entry) {
2728
entry.rc++;
2829
return entry.id;
2930
}
3031
const id = this._heapNextKey++;
3132
this._heapValueById.set(id, value);
32-
this._heapEntryByValue.set(value, { id: id, rc: 1 });
33+
if (isObject) {
34+
this._heapEntryByValue.set(value, { id: id, rc: 1 });
35+
}
3336
return id;
3437
}
3538

3639
release(ref: ref) {
3740
const value = this._heapValueById.get(ref);
38-
const entry = this._heapEntryByValue.get(value)!;
39-
entry.rc--;
40-
if (entry.rc != 0) return;
41-
42-
this._heapEntryByValue.delete(value);
43-
this._heapValueById.delete(ref);
41+
const isObject = typeof value == "object";
42+
if (isObject) {
43+
const entry = this._heapEntryByValue.get(value)!;
44+
entry.rc--;
45+
if (entry.rc != 0) return;
46+
47+
this._heapEntryByValue.delete(value);
48+
this._heapValueById.delete(ref);
49+
} else {
50+
this._heapValueById.delete(ref);
51+
}
4452
}
4553

4654
referenceHeap(ref: ref) {

Sources/JavaScriptKit/ConvertibleToJSValue.swift

-5
Original file line numberDiff line numberDiff line change
@@ -207,8 +207,6 @@ extension RawJSValue: ConvertibleToJSValue {
207207
return .undefined
208208
case .function:
209209
return .function(JSFunction(id: UInt32(payload1)))
210-
case .symbol:
211-
return .symbol(JSSymbol(id: UInt32(payload1)))
212210
}
213211
}
214212
}
@@ -240,9 +238,6 @@ extension JSValue {
240238
case let .function(functionRef):
241239
kind = .function
242240
payload1 = JavaScriptPayload1(functionRef.id)
243-
case let .symbol(symbolRef):
244-
kind = .symbol
245-
payload1 = JavaScriptPayload1(symbolRef.id)
246241
}
247242
let rawValue = RawJSValue(kind: kind, payload1: payload1, payload2: payload2)
248243
return body(rawValue)

Sources/JavaScriptKit/FundamentalObjects/JSFunction.swift

+9-8
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,15 @@ import _CJavaScriptKit
1111
/// ```
1212
///
1313
public class JSFunction: JSObject {
14+
1415
/// Call this function with given `arguments` and binding given `this` as context.
1516
/// - Parameters:
1617
/// - this: The value to be passed as the `this` parameter to this function.
1718
/// - arguments: Arguments to be passed to this function.
1819
/// - Returns: The result of this call.
1920
@discardableResult
2021
public func callAsFunction(this: JSObject? = nil, arguments: [ConvertibleToJSValue]) -> JSValue {
21-
invokeNonThrowingJSFunction(self, arguments: arguments, this: this).jsValue
22+
invokeNonThrowingJSFunction(self, arguments: arguments, this: this)
2223
}
2324

2425
/// A variadic arguments version of `callAsFunction`.
@@ -40,7 +41,7 @@ public class JSFunction: JSObject {
4041
public func new(arguments: [ConvertibleToJSValue]) -> JSObject {
4142
arguments.withRawJSValues { rawValues in
4243
rawValues.withUnsafeBufferPointer { bufferPointer in
43-
JSObject(id: _call_new(self.id, bufferPointer.baseAddress!, Int32(bufferPointer.count)))
44+
return JSObject(id: _call_new(self.id, bufferPointer.baseAddress!, Int32(bufferPointer.count)))
4445
}
4546
}
4647
}
@@ -74,7 +75,7 @@ public class JSFunction: JSObject {
7475
fatalError("unavailable")
7576
}
7677

77-
override public class func construct(from value: JSValue) -> Self? {
78+
public override class func construct(from value: JSValue) -> Self? {
7879
return value.function as? Self
7980
}
8081

@@ -83,18 +84,18 @@ public class JSFunction: JSObject {
8384
}
8485
}
8586

86-
func invokeNonThrowingJSFunction(_ jsFunc: JSFunction, arguments: [ConvertibleToJSValue], this: JSObject?) -> RawJSValue {
87+
private func invokeNonThrowingJSFunction(_ jsFunc: JSFunction, arguments: [ConvertibleToJSValue], this: JSObject?) -> JSValue {
8788
arguments.withRawJSValues { rawValues in
88-
rawValues.withUnsafeBufferPointer { bufferPointer in
89+
rawValues.withUnsafeBufferPointer { bufferPointer -> (JSValue) in
8990
let argv = bufferPointer.baseAddress
9091
let argc = bufferPointer.count
9192
var kindAndFlags = JavaScriptValueKindAndFlags()
9293
var payload1 = JavaScriptPayload1()
9394
var payload2 = JavaScriptPayload2()
9495
if let thisId = this?.id {
9596
_call_function_with_this_no_catch(thisId,
96-
jsFunc.id, argv, Int32(argc),
97-
&kindAndFlags, &payload1, &payload2)
97+
jsFunc.id, argv, Int32(argc),
98+
&kindAndFlags, &payload1, &payload2)
9899
} else {
99100
_call_function_no_catch(
100101
jsFunc.id, argv, Int32(argc),
@@ -103,7 +104,7 @@ func invokeNonThrowingJSFunction(_ jsFunc: JSFunction, arguments: [ConvertibleTo
103104
}
104105
assert(!kindAndFlags.isException)
105106
let result = RawJSValue(kind: kindAndFlags.kind, payload1: payload1, payload2: payload2)
106-
return result
107+
return result.jsValue()
107108
}
108109
}
109110
}

Sources/JavaScriptKit/FundamentalObjects/JSObject.swift

-8
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,6 @@ public class JSObject: Equatable {
9595
set { setJSValue(this: self, index: Int32(index), value: newValue) }
9696
}
9797

98-
/// Access the `symbol` member dynamically through JavaScript and Swift runtime bridge library.
99-
/// - Parameter symbol: The name of this object's member to access.
100-
/// - Returns: The value of the `name` member of this object.
101-
public subscript(_ name: JSSymbol) -> JSValue {
102-
get { getJSValue(this: self, symbol: name) }
103-
set { setJSValue(this: self, symbol: name, value: newValue) }
104-
}
105-
10698
/// A modifier to call methods as throwing methods capturing `this`
10799
///
108100
///

Sources/JavaScriptKit/FundamentalObjects/JSSymbol.swift

-56
This file was deleted.

Sources/JavaScriptKit/JSValue.swift

+22-44
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ public enum JSValue: Equatable {
1010
case null
1111
case undefined
1212
case function(JSFunction)
13-
case symbol(JSSymbol)
1413

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

71-
public var symbol: JSSymbol? {
72-
switch self {
73-
case let .symbol(symbol): return symbol
74-
default: return nil
75-
}
76-
}
77-
7870
/// Returns the `true` if this JS value is null.
7971
/// If not, returns `false`.
8072
public var isNull: Bool {
@@ -88,38 +80,39 @@ public enum JSValue: Equatable {
8880
}
8981
}
9082

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

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

10597
/// An unsafe convenience method of `JSObject.subscript(_ index: Int) -> JSValue`
10698
/// - Precondition: `self` must be a JavaScript Object.
107-
subscript(_ index: Int) -> JSValue {
99+
public subscript(_ index: Int) -> JSValue {
108100
get { object![index] }
109101
set { object![index] = newValue }
110102
}
111103
}
112104

113105
extension JSValue: Swift.Error {}
114106

115-
public extension JSValue {
116-
func fromJSValue<Type>() -> Type? where Type: ConstructibleFromJSValue {
107+
extension JSValue {
108+
public func fromJSValue<Type>() -> Type? where Type: ConstructibleFromJSValue {
117109
return Type.construct(from: self)
118110
}
119111
}
120112

121-
public extension JSValue {
122-
static func string(_ value: String) -> JSValue {
113+
extension JSValue {
114+
115+
public static func string(_ value: String) -> JSValue {
123116
.string(JSString(value))
124117
}
125118

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

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

179172
extension JSValue: ExpressibleByNilLiteral {
180-
public init(nilLiteral _: ()) {
173+
public init(nilLiteral: ()) {
181174
self = .null
182175
}
183176
}
@@ -212,28 +205,14 @@ public func setJSValue(this: JSObject, index: Int32, value: JSValue) {
212205
}
213206
}
214207

215-
public func getJSValue(this: JSObject, symbol: JSSymbol) -> JSValue {
216-
var rawValue = RawJSValue()
217-
_get_prop(this.id, symbol.id,
218-
&rawValue.kind,
219-
&rawValue.payload1, &rawValue.payload2)
220-
return rawValue.jsValue
221-
}
222-
223-
public func setJSValue(this: JSObject, symbol: JSSymbol, value: JSValue) {
224-
value.withRawJSValue { rawValue in
225-
_set_prop(this.id, symbol.id, rawValue.kind, rawValue.payload1, rawValue.payload2)
226-
}
227-
}
228-
229-
public extension JSValue {
230-
/// Return `true` if this value is an instance of the passed `constructor` function.
231-
/// Returns `false` for everything except objects and functions.
232-
/// - Parameter constructor: The constructor function to check.
233-
/// - Returns: The result of `instanceof` in the JavaScript environment.
234-
func isInstanceOf(_ constructor: JSFunction) -> Bool {
208+
extension JSValue {
209+
/// Return `true` if this value is an instance of the passed `constructor` function.
210+
/// Returns `false` for everything except objects and functions.
211+
/// - Parameter constructor: The constructor function to check.
212+
/// - Returns: The result of `instanceof` in the JavaScript environment.
213+
public func isInstanceOf(_ constructor: JSFunction) -> Bool {
235214
switch self {
236-
case .boolean, .string, .number, .null, .undefined, .symbol:
215+
case .boolean, .string, .number, .null, .undefined:
237216
return false
238217
case let .object(ref):
239218
return ref.isInstanceOf(constructor)
@@ -248,12 +227,11 @@ extension JSValue: CustomStringConvertible {
248227
switch self {
249228
case let .boolean(boolean):
250229
return boolean.description
251-
case let .string(string):
230+
case .string(let string):
252231
return string.description
253-
case let .number(number):
232+
case .number(let number):
254233
return number.description
255-
case let .object(object), let .function(object as JSObject),
256-
.symbol(let object as JSObject):
234+
case .object(let object), .function(let object as JSObject):
257235
return object.toString!().fromJSValue()!
258236
case .null:
259237
return "null"

0 commit comments

Comments
 (0)