Skip to content

Commit

Permalink
Gardening tests (#133)
Browse files Browse the repository at this point in the history
* Update package-lock.json

* Ensure that setting nested prop works

* Fix example Makefile

* Note that fatal error outputs are expected

* npm audit fix

* Add failing tests for TODO

* Add utility assert function for closure crash
  • Loading branch information
kateinoigakukun authored Jul 18, 2021
1 parent b19e7c8 commit 7f41fb4
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 24 deletions.
2 changes: 1 addition & 1 deletion Example/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ JavaScriptKitExample:

dist/JavaScriptKitExample.wasm: JavaScriptKitExample
mkdir -p dist
cp ./JavaScriptKitExample/.build/debug/JavaScriptKitExample $@
cp ./JavaScriptKitExample/.build/debug/JavaScriptKitExample.wasm $@

node_modules:
npm ci
Expand Down
24 changes: 12 additions & 12 deletions Example/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 23 additions & 4 deletions IntegrationTests/TestSuites/Sources/PrimaryTests/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,12 @@ try test("Function Call") {
let evalClosure = JSObject.global.globalObject1.eval_closure.function!

try test("Closure Lifetime") {
func expectCrashByCall(ofClosure c: JSClosureProtocol) throws {
print("======= BEGIN OF EXPECTED FATAL ERROR =====")
_ = try expectThrow(try evalClosure.throws(c))
print("======= END OF EXPECTED FATAL ERROR =======")
}

do {
let c1 = JSClosure { arguments in
return arguments[0]
Expand All @@ -200,7 +206,7 @@ try test("Closure Lifetime") {
}
c1.release()
// Call a released closure
_ = try expectThrow(try evalClosure.throws(c1))
try expectCrashByCall(ofClosure: c1)
}

do {
Expand All @@ -209,7 +215,7 @@ try test("Closure Lifetime") {
_ = JSClosure { _ in .undefined }
return .undefined
}
_ = try expectThrow(try evalClosure.throws(c1))
try expectCrashByCall(ofClosure: c1)
c1.release()
}

Expand All @@ -219,7 +225,7 @@ try test("Closure Lifetime") {
}
try expectEqual(evalClosure(c1), .boolean(true))
// second call will cause `fatalError` that can be caught as a JavaScript exception
_ = try expectThrow(try evalClosure.throws(c1))
try expectCrashByCall(ofClosure: c1)
// OneshotClosure won't call fatalError even if it's deallocated before `release`
}
}
Expand Down Expand Up @@ -617,12 +623,25 @@ try test("Error") {
}

try test("JSValue accessor") {
let globalObject1 = JSObject.global.globalObject1
var 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))

globalObject1.prop_1.nested_prop = "bar"
try expectEqual(globalObject1.prop_1.nested_prop, .string("bar"))

/* TODO: Fix https://github.com/swiftwasm/JavaScriptKit/issues/132 and un-comment this test
`nested` should not be set again to `target.nested` by `target.nested.prop = .number(1)`

let observableObj = globalObject1.observable_obj.object!
observableObj.set_called = .boolean(false)
observableObj.target.nested.prop = .number(1)
try expectEqual(observableObj.set_called, .boolean(false))

*/
}

try test("Exception") {
Expand Down
16 changes: 14 additions & 2 deletions IntegrationTests/bin/primary-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,21 @@ global.globalObject1 = {
throw 3.0
},
},
eval_closure: function(fn) {
eval_closure: function (fn) {
return fn(arguments[1])
}
},
observable_obj: {
set_called: false,
target: new Proxy({
nested: {}
}, {
set(target, key, value) {
global.globalObject1.observable_obj.set_called = true;
target[key] = value;
return true;
}
})
},
};

global.Animal = function (name, age, isCat) {
Expand Down
14 changes: 9 additions & 5 deletions IntegrationTests/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 7f41fb4

Please sign in to comment.