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
7 changes: 6 additions & 1 deletion componentize.sh.in
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ aot=@AOT@
preopen_dir="${PREOPEN_DIR:-}"

usage() {
echo "Usage: $(basename "$0") [--verbose] [-i,--initializer-script-path] [--legacy-script] [input.js] [-o output.wasm]"
echo "Usage: $(basename "$0") [--verbose] [-i,--initializer-script-path path] [--strip-path-prefix prefix] [--legacy-script] [input.js] [-o output.wasm]"
echo " Providing an input file but no output uses the input base name with a .wasm extension"
echo " Providing an output file but no input creates a component without running any top-level script"
echo " Specifying '--verbose' causes the detailed output during initialization and execution"
echo " Specifying '-i' or '--initializer-script-path' allows specifying an initializer script"
echo " Specifying '--strip-path-prefix' will cause the provided prefix to be stripped from paths in stack traces and the debugger"
echo " Specifying '--legacy-script' causes evaluation as a legacy JS script instead of a module"
exit 1
}
Expand Down Expand Up @@ -45,6 +46,10 @@ do
STARLING_ARGS="$STARLING_ARGS $1 $2"
shift 2
;;
--strip-path-prefix)
STARLING_ARGS="$STARLING_ARGS $1 $2"
shift 2
;;
-v|--verbose)
STARLING_ARGS="$1 $STARLING_ARGS"
VERBOSE=1
Expand Down
5 changes: 5 additions & 0 deletions include/config-parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ class ConfigParser {
config_->verbose = true;
} else if (args[i] == "-d" || args[i] == "--enable-script-debugging") {
config_->debugging = true;
} else if (args[i] == "--strip-path-prefix") {
if (i + 1 < args.size()) {
config_->path_prefix = mozilla::Some(args[i + 1]);
i++;
}
} else if (args[i] == "--legacy-script") {
config_->module_mode = false;
if (i + 1 < args.size()) {
Expand Down
1 change: 1 addition & 0 deletions include/extension-api.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class AsyncTask;
struct EngineConfig {
mozilla::Maybe<std::string> content_script_path = mozilla::Nothing();
mozilla::Maybe<std::string> content_script = mozilla::Nothing();
mozilla::Maybe<std::string> path_prefix = mozilla::Nothing();
bool module_mode = true;

/**
Expand Down
6 changes: 3 additions & 3 deletions runtime/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ bool create_initializer_global(Engine *engine) {
return true;
}

bool init_js() {
bool init_js(const EngineConfig& config) {
JS_Init();

JSContext *cx = JS_NewContext(JS::DefaultHeapMaxBytes);
Expand Down Expand Up @@ -353,7 +353,7 @@ bool init_js() {
// generating bytecode for functions.
// https://searchfox.org/mozilla-central/rev/5b2d2863bd315f232a3f769f76e0eb16cdca7cb0/js/public/CompileOptions.h#571-574
opts->setForceFullParse();
scriptLoader = new ScriptLoader(ENGINE, opts);
scriptLoader = new ScriptLoader(ENGINE, opts, config.path_prefix);

// TODO: restore in a way that doesn't cause a dependency on the Performance builtin in the core runtime.
// builtins::Performance::timeOrigin.emplace(
Expand Down Expand Up @@ -451,7 +451,7 @@ Engine::Engine(std::unique_ptr<EngineConfig> config) {

TRACE("StarlingMonkey engine initializing");

if (!init_js()) {
if (!init_js(*config_.get())) {
abort("Initializing JS Engine");
}

Expand Down
25 changes: 16 additions & 9 deletions runtime/script_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ JS::PersistentRootedObject moduleRegistry;
JS::PersistentRootedObject builtinModules;
static bool MODULE_MODE = true;
static char* BASE_PATH = nullptr;
mozilla::Maybe<std::string> PATH_PREFIX = mozilla::Nothing();
JS::CompileOptions *COMPILE_OPTS;

using host_api::HostString;
Expand Down Expand Up @@ -43,15 +44,19 @@ class AutoCloseFile {
}
};

// strip off base when possible for nicer debugging stacks
static const char* strip_base(const char* resolved_path, const char* base) {
size_t base_len = strlen(base);
if (strncmp(resolved_path, base, base_len) != 0) {
// strip off the given prefix when possible for nicer debugging stacks
static const char* strip_prefix(const char* resolved_path,
mozilla::Maybe<std::string> path_prefix) {
if (!path_prefix) {
return strdup(resolved_path);
}
auto& base = *path_prefix;
if (strncmp(resolved_path, base.data(), base.length()) != 0) {
return strdup(resolved_path);
}
size_t path_len = strlen(resolved_path);
char* buf(js_pod_malloc<char>(path_len - base_len + 1));
strncpy(buf, &resolved_path[base_len], path_len - base_len + 1);
char* buf(js_pod_malloc<char>(path_len - base.length() + 1));
strncpy(buf, &resolved_path[base.length()], path_len - base.length() + 1);
return buf;
}

Expand Down Expand Up @@ -325,7 +330,7 @@ JSObject* module_resolve_hook(JSContext* cx, HandleValue referencingPrivate,
const char* resolved_path = resolve_path(path.get(), str.ptr.get(), str.len);

JS::CompileOptions opts(cx, *COMPILE_OPTS);
opts.setFileAndLine(strip_base(resolved_path, BASE_PATH), 1);
opts.setFileAndLine(strip_prefix(resolved_path, PATH_PREFIX), 1);
return get_module(cx, path.get(), resolved_path, opts);
}

Expand All @@ -349,11 +354,13 @@ bool module_metadata_hook(JSContext* cx, HandleValue referencingPrivate, HandleO
return true;
}

ScriptLoader::ScriptLoader(api::Engine* engine, JS::CompileOptions *opts) {
ScriptLoader::ScriptLoader(api::Engine* engine, JS::CompileOptions *opts,
mozilla::Maybe<std::string> path_prefix) {
MOZ_ASSERT(!SCRIPT_LOADER);
ENGINE = engine;
SCRIPT_LOADER = this;
COMPILE_OPTS = opts;
PATH_PREFIX = std::move(path_prefix);
JSContext* cx = engine->cx();
moduleRegistry.init(cx, JS::NewMapObject(cx));
builtinModules.init(cx, JS::NewMapObject(cx));
Expand Down Expand Up @@ -452,7 +459,7 @@ bool ScriptLoader::eval_top_level_script(const char *path, JS::SourceText<mozill
JSContext *cx = ENGINE->cx();

JS::CompileOptions opts(cx, *COMPILE_OPTS);
opts.setFileAndLine(strip_base(path, BASE_PATH), 1);
opts.setFileAndLine(strip_prefix(path, PATH_PREFIX), 1);
JS::RootedScript script(cx);
RootedObject module(cx);
if (MODULE_MODE) {
Expand Down
3 changes: 2 additions & 1 deletion runtime/script_loader.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@

class ScriptLoader {
public:
ScriptLoader(api::Engine* engine, JS::CompileOptions* opts);
ScriptLoader(api::Engine *engine, JS::CompileOptions *opts,
mozilla::Maybe<std::string> path_prefix);
~ScriptLoader();

bool define_builtin_module(const char* id, HandleValue builtin);
Expand Down
8 changes: 4 additions & 4 deletions tests/e2e/runtime-err/expect_serve_stderr.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
stderr [0] :: Error while running request handler: runtime error
stderr [0] :: Stack:
stderr [0] :: @runtime-err.js:6:13
stderr [0] :: @runtime-err.js:7:7
stderr [0] :: @e2e/runtime-err/runtime-err.js:6:13
stderr [0] :: @e2e/runtime-err/runtime-err.js:7:7
stderr [0] ::
stderr [0] :: Caused by: error cause
stderr [0] :: Stack:
stderr [0] :: @runtime-err.js:6:49
stderr [0] :: @runtime-err.js:7:7
stderr [0] :: @e2e/runtime-err/runtime-err.js:6:49
stderr [0] :: @e2e/runtime-err/runtime-err.js:7:7
stderr [0] ::
2 changes: 1 addition & 1 deletion tests/e2e/syntax-err/expect_wizer_stderr.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Exception while evaluating top-level script
syntax-err.js:2:1 SyntaxError: expected expression, got end of script
e2e/syntax-err/syntax-err.js:2:1 SyntaxError: expected expression, got end of script
2 changes: 1 addition & 1 deletion tests/e2e/tla-err/expect_wizer_stderr.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Exception while evaluating top-level script
tla-err.js:3:10 Error: blah
e2e/tla-err/tla-err.js:3:10 Error: blah
6 changes: 5 additions & 1 deletion tests/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,13 @@ if [ -z "$test_component" ]; then
runtime_args="-i $test_dir/init.js $runtime_args"
fi

test_top_level="$(dirname $(dirname "$test_dir"))/"

runtime_args="--strip-path-prefix $test_top_level $runtime_args"

# Run Wizer
set +e
PREOPEN_DIR="$(dirname $(dirname "$test_dir"))" "$test_runtime/componentize.sh" $componentize_flags $runtime_args "$test_component" 1> "$stdout_log" 2> "$stderr_log"
PREOPEN_DIR="$test_top_level" "$test_runtime/componentize.sh" $componentize_flags $runtime_args "$test_component" 1> "$stdout_log" 2> "$stderr_log"
wizer_result=$?
set -e

Expand Down