Skip to content
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
2 changes: 2 additions & 0 deletions Android/core/src/main/assets/scriptwidget/prepare.js

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

Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,11 @@ enum ScriptWidgetCompiler {
static func wrappedSource(_ source: String, callFunction: String = "") -> String {
var result = "async function $main() { try {"
result += source
// A source may end in a `//` comment without a line break; what follows
// must not become part of that comment. A line break at the end moves
// no line number of the source. The fingerprint is not bumped for it:
// every artifact compiled before stays correct for its source.
result += "\n"
if !callFunction.isEmpty { result += "await \(callFunction)();" }
result += "} catch(e){ console.error(e); $error(`${e}`) } }"
return result
Expand Down
15 changes: 15 additions & 0 deletions Tests/ScriptWidgetRuntimeTests/RuntimeUnitTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -905,6 +905,21 @@ final class CompiledArtifactTests: XCTestCase {
XCTAssertTrue(artifact.javaScript.contains("$main"))
XCTAssertNotNil(ScriptWidgetCompiledArtifactStore.load(package: package, source: source))
}

/// The wrapper used to append its own closing code directly to the source,
/// so a last line that was a comment swallowed it and nothing compiled.
func testSourceEndingInALineCommentWithoutLineBreakCompiles() throws {
let source = "$render(<text>ready</text>); // the end"
let result = ScriptWidgetPrecompiler.compileNow(package: package, source: source)
guard case .success(let artifact) = result else {
return XCTFail("precompile failed: \(result)")
}
XCTAssertTrue(artifact.javaScript.contains("$main"))
XCTAssertEqual(
ScriptWidgetCompiler.wrappedSource("x // note", callFunction: "render"),
"async function $main() { try {x // note\nawait render();} catch(e){ console.error(e); $error(`${e}`) } }"
)
}
}

final class ExecutionSessionTests: XCTestCase {
Expand Down
2 changes: 2 additions & 0 deletions Web/preview-engine/src/contract.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ export function makeError(kind, message) {
export function wrappedSource(source, callFunction = "") {
let result = "async function $main() { try {";
result += source;
// A source may end in a `//` comment without a line break.
result += "\n";
if (callFunction !== "") result += `await ${callFunction}();`;
result += "} catch(e){ console.error(e); $error(`${e}`) } }";
return result;
Expand Down
4 changes: 3 additions & 1 deletion Web/preview-engine/test/contract.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,7 @@ test("native compiler fingerprint and entry wrapper match ScriptWidgetCompiler",
assert.match(packageSwift, new RegExp(`static let compilerFingerprint = "${runtimeContract.nativeCompilerFingerprint}"`));
assert.ok(runtimeSwift.includes('var result = "async function $main() { try {"'));
assert.ok(runtimeSwift.includes('result += "} catch(e){ console.error(e); $error(`${e}`) } }"'));
assert.equal(wrappedSource("X"), "async function $main() { try {X} catch(e){ console.error(e); $error(`${e}`) } }");
assert.ok(runtimeSwift.includes('result += "\\n"'));
assert.equal(wrappedSource("X"), "async function $main() { try {X\n} catch(e){ console.error(e); $error(`${e}`) } }");
assert.equal(wrappedSource("X // note", "render"), "async function $main() { try {X // note\nawait render();} catch(e){ console.error(e); $error(`${e}`) } }");
});
7 changes: 7 additions & 0 deletions Web/preview-engine/test/kernel.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ test("missing $render text yields the native placeholder element", async () => {
assert.equal(result.tree, '{"$element":"text","props":null,"children":["#UI Not Found#"]}');
});

test("a source that ends in a line comment without a line break runs", async () => {
// The wrapper's closing code used to land inside the comment.
const result = await run("$render(<text>ready</text>); // the end");
assert.equal(result.error, null);
assert.equal(result.tree, '{"$element":"text","props":null,"children":["ready"]}');
});

test("errors use the native categories", async () => {
assert.equal((await run('throw new Error("boom")')).error.kind, "scriptException");
assert.equal((await run("$render(<text>)")).error.kind, "scriptException");
Expand Down
Loading