Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
0cd5c7f
build(runtime): take the base64 codec from the shared runtime-common …
edusperoni Dec 4, 2026
1624660
feat(runtime): run the HTTP module loader on runtime-common
edusperoni Dec 5, 2026
2108347
build: pin runtime-common foundation and implement its platform funct…
edusperoni Dec 5, 2026
89b9580
feat(runtime): pump through Loop::PumpUntil
edusperoni Dec 5, 2026
2a2d735
fix(runtime): termination always ends a module load with a throw
edusperoni Dec 5, 2026
31b4eaa
build: pin runtime-common at ebb7b87
edusperoni Dec 5, 2026
2f63376
fix(runtime): V8 callbacks throw on Terminated instead of returning
edusperoni Dec 5, 2026
b6fd4b1
build: pin runtime-common at 95d96cc and implement its app-file seam
edusperoni Dec 5, 2026
32ab7ec
feat(runtime): give the code cache a directory of its own
edusperoni Dec 5, 2026
88d3a22
feat(runtime): compile app files and keep code caches through the sha…
edusperoni Dec 5, 2026
4f94119
feat(runtime): remove the code caches older runtimes left beside sources
edusperoni Dec 5, 2026
ce8edad
build: pin runtime-common at 6826386
edusperoni Dec 5, 2026
9e562df
test: let in-flight cache writes finish before checking for temporaries
edusperoni Dec 5, 2026
71a1da0
build: pin runtime-common at 39e2174 and implement its URL parser seam
edusperoni Dec 5, 2026
85a53ba
test: pin the verbatim spelling of module and script URLs
edusperoni Dec 5, 2026
4bc5dcb
feat(runtime): name modules through the shared core's module identity
edusperoni Dec 5, 2026
e9158bb
build: pin runtime-common at 3bcf603
edusperoni Dec 5, 2026
03d1633
feat(runtime): give each isolate a loader config snapshot
edusperoni Dec 5, 2026
5e3390c
feat(runtime): load modules through the shared core's registry and re…
edusperoni Dec 5, 2026
f5ed9fc
test(runtime): pin the shared resolver's specifier rules
edusperoni Dec 5, 2026
60a6ac9
build: pin runtime-common at da7852d
edusperoni Dec 5, 2026
1ee254f
fix(runtime): prefilter blob: URLs on the whole scheme
edusperoni Dec 5, 2026
63ac13e
test(runtime): pin the reviewed resolver rules
edusperoni Dec 6, 2026
83a7256
test(runtime): benchmark import() through the module loader
edusperoni Dec 6, 2026
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
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@
path = test-app/build-tools/android-dts-generator
url = https://github.com/NativeScript/android-dts-generator.git
branch = main
[submodule "test-app/runtime/src/main/cpp/runtime-common"]
path = test-app/runtime/src/main/cpp/runtime-common
url = https://github.com/NativeScript/runtime-common.git
Original file line number Diff line number Diff line change
Expand Up @@ -578,7 +578,10 @@ exports.run = function () {
// Yield to the looper between benchmarks so GC and queued work run outside timed rounds.
function next() {
if (index >= benchmarks.length) {
runContention();
require("./moduleBenchmarks").run(report, skipped, ROUNDS, SCALE, function (r) {
results.push.apply(results, r);
runContention();
});
return;
}
gc();
Expand Down
21 changes: 21 additions & 0 deletions test-app/app/src/main/assets/app/tests/benchmarks/modbench/hub.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { v0 } from "./m0.mjs";
import { v1 } from "./m1.mjs";
import { v2 } from "./m2.mjs";
import { v3 } from "./m3.mjs";
import { v4 } from "./m4.mjs";
import { v5 } from "./m5.mjs";
import { v6 } from "./m6.mjs";
import { v7 } from "./m7.mjs";
import { v8 } from "./m8.mjs";
import { v9 } from "./m9.mjs";
import { v10 } from "./m10.mjs";
import { v11 } from "./m11.mjs";
import { v12 } from "./m12.mjs";
import { v13 } from "./m13.mjs";
import { v14 } from "./m14.mjs";
import { v15 } from "./m15.mjs";
import { v16 } from "./m16.mjs";
import { v17 } from "./m17.mjs";
import { v18 } from "./m18.mjs";
import { v19 } from "./m19.mjs";
export const sum = v0 + v1 + v2 + v3 + v4 + v5 + v6 + v7 + v8 + v9 + v10 + v11 + v12 + v13 + v14 + v15 + v16 + v17 + v18 + v19;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const name = "leaf";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const v0 = 0;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const v1 = 1;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const v10 = 10;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const v11 = 11;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const v12 = 12;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const v13 = 13;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const v14 = 14;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const v15 = 15;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const v16 = 16;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const v17 = 17;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const v18 = 18;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const v19 = 19;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const v2 = 2;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const v3 = 3;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const v4 = 4;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const v5 = 5;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const v6 = 6;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const v7 = 7;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const v8 = 8;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const v9 = 9;
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Module loader rows for the JniBench harness: import() of cached modules
// through each specifier form, and links of a fresh module whose 20 static
// imports are cached. Async, so each row awaits every import.

function rows(appRoot) {
var leafPath = appRoot + "/tests/benchmarks/modbench/leaf.mjs";
var hubSeq = 0;
return [
["module.import() cached relative", 2000, function () { return import("./modbench/leaf.mjs"); }],
["module.import() cached bare mapped", 2000, function () { return import("ns-modbench-leaf"); }],
["module.import() cached app-relative", 2000, function () { return import("~/tests/benchmarks/modbench/leaf.mjs"); }],
["module.import() cached absolute path", 2000, function () { return import(leafPath); }],
["module.import() cached file URL", 2000, function () { return import("file://" + leafPath); }],
["module.import() fresh hub, 20 cached static imports", 200, function () {
return import("./modbench/hub.mjs?i=" + (hubSeq++));
}]
];
}

exports.run = function (report, skipped, rounds, scale, done) {
// The runtime's own spelling of the app root; getFilesDir() may spell it
// through another mount (/data/user/0 for /data/data).
var appRoot = __dirname.slice(0, __dirname.length - "/tests/benchmarks".length);
require("ns:module").configureLoader({ importMap: { imports: { "ns-modbench-leaf": "~/tests/benchmarks/modbench/leaf.mjs" } } });
var list = rows(appRoot).filter(function (row) { return !skipped(row[0]); });
var results = [];
function loop(body, n) {
var i = 0;
return new Promise(function (resolve, reject) {
function step() {
if (i >= n) { resolve(); return; }
i++;
body().then(step, reject);
}
step();
});
}
function runRow(index) {
if (index >= list.length) { done(results); return; }
var row = list[index];
var n = row[1] * scale;
var samples = [];
var r = 0;
loop(row[2], Math.max(200, n >> 2)).then(function round() {
if (r >= rounds) {
results.push(report(row[0], samples, null, n * rounds));
setTimeout(function () { runRow(index + 1); }, 50);
return;
}
r++;
var start = performance.now();
return loop(row[2], n).then(function () {
samples.push(((performance.now() - start) * 1e6) / n);
return round();
});
}).catch(function (e) {
console.log("JNIBENCH_ERROR " + row[0] + ": " + e);
runRow(index + 1);
});
}
runRow(0);
};
140 changes: 140 additions & 0 deletions test-app/app/src/main/assets/app/tests/codeCacheFiles.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
// The runtime's on-disk code caches, as the shared core lays them out:
// <code_cache>/v8/<16 hex digits of the FNV-1a 64 hash of the key>, then
// ".mcache" for an ES module or ".cache" for a classic script. The key is the
// source's path relative to the app root, or its absolute path outside it.
// A file is a header (magic "NSCC", version, kind, key length, source size,
// freshness token length, payload length, key, token), then V8's payload.
var context = com.tns.NativeScriptApplication.getInstance();
var dir = String(new java.io.File(context.getCodeCacheDir(), "v8").getAbsolutePath());
var appRoot = String(context.getFilesDir().getCanonicalPath()) + "/app";

var FNV_OFFSET = BigInt("0xcbf29ce484222325");
var FNV_PRIME = BigInt("0x100000001b3");
var MASK_64 = (BigInt(1) << BigInt(64)) - BigInt(1);

function fnv1a64(text) {
var bytes = unescape(encodeURIComponent(text));
var hash = FNV_OFFSET;
for (var i = 0; i < bytes.length; i++) {
hash = ((hash ^ BigInt(bytes.charCodeAt(i))) * FNV_PRIME) & MASK_64;
}
var hex = hash.toString(16);
while (hex.length < 16) {
hex = "0" + hex;
}
return hex;
}

function keyFor(path) {
return path.indexOf(appRoot + "/") === 0 ? path.substring(appRoot.length + 1) : path;
}

// `kind` is "module" or "script".
function fileFor(path, kind) {
return dir + "/" + fnv1a64(keyFor(path)) + (kind === "script" ? ".cache" : ".mcache");
}

// What platform::Stat reports as the source's freshness token: its mtime with
// nanoseconds.
function sourceToken(path) {
var mtime = android.system.Os.stat(path).st_mtim;
var nanos = String(mtime.tv_nsec);
while (nanos.length < 9) {
nanos = "0" + nanos;
}
return String(mtime.tv_sec) + "." + nanos;
}

function readBytes(file) {
var f = new java.io.File(file);
return f.exists() ? java.nio.file.Files.readAllBytes(f.toPath()) : null;
}

// The cache file's header, or null when there is no file or it is not a cache.
function readHeader(file) {
var bytes = readBytes(file);
if (bytes === null || bytes.length < 32) {
return null;
}
var u8 = function (i) {
return bytes[i] & 0xff;
};
var u32 = function (i) {
return (u8(i) | (u8(i + 1) << 8) | (u8(i + 2) << 16) | (u8(i + 3) << 24)) >>> 0;
};
var text = function (start, length) {
var out = "";
for (var i = start; i < start + length; i++) {
out += String.fromCharCode(u8(i));
}
return decodeURIComponent(escape(out));
};
if (text(0, 4) !== "NSCC") {
return null;
}
var keyLength = u32(12);
var tokenLength = u32(24);
var payloadStart = 32 + keyLength + tokenLength;
var zeroPayload = true;
for (var i = payloadStart; i < bytes.length && zeroPayload; i++) {
zeroPayload = bytes[i] === 0;
}
return {
key: text(32, keyLength),
sourceSize: u32(16) + u32(20) * 4294967296,
token: text(32 + keyLength, tokenLength),
payloadStart: payloadStart,
payloadLength: u32(28),
fileLength: bytes.length,
zeroPayload: zeroPayload
};
}

// Whether `file` is a cache for the source at `path` as it is now.
function isFresh(file, path) {
var header = readHeader(file);
return header !== null && header.key === keyFor(path) &&
header.sourceSize === new java.io.File(path).length() &&
header.token === sourceToken(path) &&
header.fileLength === header.payloadStart + header.payloadLength;
}

// Overwrites the payload with zeros and keeps the header, so the cache still
// reads as fresh and V8 rejects it.
function zeroPayload(file) {
var header = readHeader(file);
var bytes = readBytes(file);
for (var i = header.payloadStart; i < bytes.length; i++) {
bytes[i] = 0;
}
var out = new java.io.FileOutputStream(file);
try {
out.write(bytes);
} finally {
out.close();
}
}

// Names in the cache directory that start with `file`'s name and a dot: the
// writer's temporaries.
function temporariesOf(file) {
var name = file.substring(file.lastIndexOf("/") + 1) + ".";
var names = new java.io.File(dir).list();
var found = [];
for (var i = 0; names && i < names.length; i++) {
if (String(names[i]).indexOf(name) === 0) {
found.push(String(names[i]));
}
}
return found;
}

module.exports = {
dir: dir,
fileFor: fileFor,
sourceToken: sourceToken,
readHeader: readHeader,
isFresh: isFresh,
zeroPayload: zeroPayload,
temporariesOf: temporariesOf
};
24 changes: 24 additions & 0 deletions test-app/app/src/main/assets/app/tests/requireExceptionTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,4 +161,28 @@ describe("Tests require exceptions ", function () {

expect(exceptionCaught).toBe(false);
});

it("throws when a script that exists cannot be read", function () {
var name = "unreadable-" + Date.now() + ".js";
var path = __dirname + "/" + name;
var file = new java.io.File(path);
var writer = new java.io.FileWriter(file);
try {
writer.write("module.exports = { loaded: true };");
} finally {
writer.close();
}
file.setReadable(false, false);
var message;
try {
require(path);
message = "loaded";
} catch (e) {
message = String(e.message || e);
} finally {
file.delete();
}
expect(message).toContain("Cannot read module ");
expect(message).toContain(name);
});
});
Loading