Skip to content

Commit

Permalink
feat: performance builtin support (#150)
Browse files Browse the repository at this point in the history
  • Loading branch information
guybedford authored Oct 29, 2024
1 parent 8995b35 commit 5197559
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 2 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ cmake_minimum_required(VERSION 3.27)
include("StarlingMonkey/cmake/add_as_subproject.cmake")

add_builtin(componentize::embedding SRC embedding/embedding.cpp)
include_directories("StarlingMonkey")

project(ComponentizeJS)
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ lib/starlingmonkey_embedding.wasm: StarlingMonkey/cmake/* embedding/* StarlingMo
@cp build-release/starling-raw.wasm/starling-raw.wasm $@

lib/starlingmonkey_embedding_weval.wasm: StarlingMonkey/cmake/* embedding/* StarlingMonkey/runtime/* StarlingMonkey/builtins/* StarlingMonkey/builtins/*/* StarlingMonkey/builtins/*/*/* StarlingMonkey/include/* | lib
cmake -B build-release-weval -DCMAKE_BUILD_TYPE=Release -DWEVAL=ON
cmake -B build-release-weval -DCMAKE_BUILD_TYPE=Release -DUSE_WASM_OPT=OFF -DWEVAL=ON
make -j16 -C build-release-weval
@cp build-release-weval/starling-raw.wasm/starling-raw.wasm $@

Expand Down
13 changes: 12 additions & 1 deletion embedding/embedding.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "embedding.h"
#include "builtins/web/performance.h"

namespace builtins::web::console {

Expand Down Expand Up @@ -65,7 +66,8 @@ from_bigint64(JS::MutableHandleValue handle) {
JS::BigInt *arg0 = handle.toBigInt();
uint64_t arg0_uint64;
if (!JS::detail::BigIntIsUint64(arg0, &arg0_uint64)) {
Runtime.engine->abort("Internal bindgen error in coreabi_from_bigint64 validation");
Runtime.engine->abort(
"Internal bindgen error in coreabi_from_bigint64 validation");
}
return arg0_uint64;
}
Expand Down Expand Up @@ -148,6 +150,10 @@ __attribute__((export_name("call"))) uint32_t call(uint32_t fn_idx,
if (Runtime.first_call) {
js::ResetMathRandomSeed(Runtime.cx);
Runtime.first_call = false;
if (Runtime.clocks) {
builtins::web::performance::Performance::timeOrigin.emplace(
std::chrono::high_resolution_clock::now());
}
}
if (Runtime.cur_fn_idx != -1) {
LOG("(call) unexpected call state, post_call was not called after last "
Expand Down Expand Up @@ -350,6 +356,11 @@ componentize_initialize() {
Runtime.debug = true;
}

uint32_t feature_clocks = atoi(getenv("FEATURE_CLOCKS"));
if (feature_clocks) {
Runtime.clocks = true;
}

__wizer_initialize();
char env_name[100];
LOG("(wizer) retrieve and generate the export bindings");
Expand Down
2 changes: 2 additions & 0 deletions embedding/embedding.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ namespace componentize::embedding
bool debug = false;
bool first_call = true;

bool clocks = false;

JSContext *cx;

InitError init_err = InitError::OK;
Expand Down
1 change: 1 addition & 0 deletions src/componentize.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ export async function componentize(jsSource, witWorld, opts) {
SOURCE_NAME: sourceName,
IMPORT_WRAPPER_CNT: Object.keys(importWrappers).length.toString(),
EXPORT_CNT: exports.length.toString(),
FEATURE_CLOCKS: features.includes('clocks') ? '1' : '',
};

for (const [idx, [export_name, expt]] of exports.entries()) {
Expand Down
19 changes: 19 additions & 0 deletions test/builtins/performance-disabled.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { ok, strictEqual } from 'node:assert';

export const source = `
export function run () {
performance.now();
};
`;

export const disableFeatures = ['clocks'];

export async function test (run) {
try {
const { stdout, stderr } = await run();
ok(false);
} catch {
// performance builtin just panics
// (yes we can and should do better...)
}
}
28 changes: 28 additions & 0 deletions test/builtins/performance.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { ok, strictEqual } from 'node:assert';

export const source = `
export function run () {
const start = performance.now();
let previous = 0, cur = 1;
for (let i = 1; i < 1000; i++) {
const tmp = cur;
cur = previous + cur;
previous = tmp;
}
const end = performance.now();
console.log('Calculated fib 1000: ' + cur);
console.error((end - start) + ' ms');
};
`;

export async function test (run) {
const { stdout, stderr } = await run();
strictEqual(stdout, 'Calculated fib 1000: 4.346655768693743e+208\n');

ok(stderr.includes(' ms'));
const time = Number(stderr.split(' ms')[0]);
// TODO: fix back to half a millisecond when Weval fix is added
if (time > 3) {
throw new Error('took more than half a millisecond - ' + time + ' ms');
}
}

0 comments on commit 5197559

Please sign in to comment.