Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
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
21 changes: 21 additions & 0 deletions 21 IntegrationTests/TestSuites/Sources/PrimaryTests/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -268,14 +268,26 @@ try test("Call Function With This") {
// ```
let objectConstructor = try expectFunction(getJSValue(this: .global, name: "Animal"))
let cat1 = objectConstructor.new("Tama", 3, true)
let cat1Value = JSValue.object(cat1)
let getIsCat = try expectFunction(getJSValue(this: cat1, name: "getIsCat"))
let setName = try expectFunction(getJSValue(this: cat1, name: "setName"))

// Direct call without this
try expectEqual(getIsCat(), .undefined)

// Call with this
let gotIsCat = getIsCat(this: cat1)
try expectEqual(gotIsCat, .boolean(true))
try expectEqual(cat1.getIsCat!(), .boolean(true))
try expectEqual(cat1Value.getIsCat(), .boolean(true))

// Call with this and argument
setName(this: cat1, JSValue.string("Shiro"))
try expectEqual(getJSValue(this: cat1, name: "name"), .string("Shiro"))
_ = cat1.setName!("Tora")
try expectEqual(getJSValue(this: cat1, name: "name"), .string("Tora"))
_ = cat1Value.setName("Chibi")
try expectEqual(getJSValue(this: cat1, name: "name"), .string("Chibi"))
}

try test("Object Conversion") {
Expand Down Expand Up @@ -496,3 +508,12 @@ try test("Error") {
try expectEqual(JSError(from: .string("error"))?.description, nil)
try expectEqual(JSError(from: .object(error.jsObject))?.description, expectedDescription)
}

try test("JSValue accessor") {
let globalObject1 = JSObject.global.globalObject1
try expectEqual(globalObject1.prop_1.nested_prop, .number(1))
try expectEqual(globalObject1.object!.prop_1.object!.nested_prop, .number(1))

try expectEqual(globalObject1.prop_4[0], .number(3))
try expectEqual(globalObject1.prop_4[1], .number(4))
}
3 changes: 3 additions & 0 deletions 3 IntegrationTests/bin/primary-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ global.Animal = function (name, age, isCat) {
this.getIsCat = function () {
return this.isCat;
};
this.setName = function (name) {
this.name = name;
}
};

const { startWasiTask } = require("../lib");
Expand Down
23 changes: 23 additions & 0 deletions 23 Sources/JavaScriptKit/JSValue.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import _CJavaScriptKit

/// `JSValue` represents a value in JavaScript.
@dynamicMemberLookup
public enum JSValue: Equatable {
case boolean(Bool)
case string(JSString)
Expand Down Expand Up @@ -79,6 +80,28 @@ public enum JSValue: Equatable {
}
}

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) {
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 {
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 {
get { object![index] }
set { object![index] = newValue }
}
Comment thread
kateinoigakukun marked this conversation as resolved.
}

extension JSValue {
public func fromJSValue<Type>() -> Type? where Type: ConstructibleFromJSValue {
return Type.construct(from: self)
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.