Skip to content

Commit 337c2ba

Browse files
committed
Support stripping a prefix from all file paths
This introduces the CLI option `--strip-path-prefix prefix` and strips the provided `prefix` from all paths as seen by content, in stack traces, and the debugger. Previously, we unilaterally stripped the directory part of the top-level script's path instead. This new approach is better in two ways: 1. it works for files that are in ancestor or sibling directories to the top-level script 2. it enables using an ancestor directory as the root The second of these advantages can be seen in the diffs to the test expectations files: the stacks for those are more informative, because they now include the directory of the actual test, since we're only stripping the `[StarlingMonkey root]/tests` part of the path.
1 parent 53044ec commit 337c2ba

File tree

10 files changed

+44
-21
lines changed

10 files changed

+44
-21
lines changed

componentize.sh.in

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@ aot=@AOT@
99
preopen_dir="${PREOPEN_DIR:-}"
1010

1111
usage() {
12-
echo "Usage: $(basename "$0") [--verbose] [-i,--initializer-script-path] [--legacy-script] [input.js] [-o output.wasm]"
12+
echo "Usage: $(basename "$0") [--verbose] [-i,--initializer-script-path path] [--strip-path-prefix prefix] [--legacy-script] [input.js] [-o output.wasm]"
1313
echo " Providing an input file but no output uses the input base name with a .wasm extension"
1414
echo " Providing an output file but no input creates a component without running any top-level script"
1515
echo " Specifying '--verbose' causes the detailed output during initialization and execution"
1616
echo " Specifying '-i' or '--initializer-script-path' allows specifying an initializer script"
17+
echo " Specifying '--strip-path-prefix' will cause the provided prefix to be stripped from paths in stack traces and the debugger"
1718
echo " Specifying '--legacy-script' causes evaluation as a legacy JS script instead of a module"
1819
exit 1
1920
}
@@ -45,6 +46,10 @@ do
4546
STARLING_ARGS="$STARLING_ARGS $1 $2"
4647
shift 2
4748
;;
49+
--strip-path-prefix)
50+
STARLING_ARGS="$STARLING_ARGS $1 $2"
51+
shift 2
52+
;;
4853
-v|--verbose)
4954
STARLING_ARGS="$1 $STARLING_ARGS"
5055
VERBOSE=1

include/config-parser.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,11 @@ class ConfigParser {
8787
config_->verbose = true;
8888
} else if (args[i] == "-d" || args[i] == "--enable-script-debugging") {
8989
config_->debugging = true;
90+
} else if (args[i] == "--strip-path-prefix") {
91+
if (i + 1 < args.size()) {
92+
config_->path_prefix = mozilla::Some(args[i + 1]);
93+
i++;
94+
}
9095
} else if (args[i] == "--legacy-script") {
9196
config_->module_mode = false;
9297
if (i + 1 < args.size()) {

include/extension-api.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ class AsyncTask;
3737
struct EngineConfig {
3838
mozilla::Maybe<std::string> content_script_path = mozilla::Nothing();
3939
mozilla::Maybe<std::string> content_script = mozilla::Nothing();
40+
mozilla::Maybe<std::string> path_prefix = mozilla::Nothing();
4041
bool module_mode = true;
4142

4243
/**

runtime/engine.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ bool create_initializer_global(Engine *engine) {
313313
return true;
314314
}
315315

316-
bool init_js() {
316+
bool init_js(const EngineConfig& config) {
317317
JS_Init();
318318

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

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

452452
TRACE("StarlingMonkey engine initializing");
453453

454-
if (!init_js()) {
454+
if (!init_js(*config_.get())) {
455455
abort("Initializing JS Engine");
456456
}
457457

runtime/script_loader.cpp

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ JS::PersistentRootedObject moduleRegistry;
1515
JS::PersistentRootedObject builtinModules;
1616
static bool MODULE_MODE = true;
1717
static char* BASE_PATH = nullptr;
18+
mozilla::Maybe<std::string> PATH_PREFIX = mozilla::Nothing();
1819
JS::CompileOptions *COMPILE_OPTS;
1920

2021
using host_api::HostString;
@@ -43,15 +44,19 @@ class AutoCloseFile {
4344
}
4445
};
4546

46-
// strip off base when possible for nicer debugging stacks
47-
static const char* strip_base(const char* resolved_path, const char* base) {
48-
size_t base_len = strlen(base);
49-
if (strncmp(resolved_path, base, base_len) != 0) {
47+
// strip off the given prefix when possible for nicer debugging stacks
48+
static const char* strip_prefix(const char* resolved_path,
49+
mozilla::Maybe<std::string> path_prefix) {
50+
if (!path_prefix) {
51+
return strdup(resolved_path);
52+
}
53+
auto& base = *path_prefix;
54+
if (strncmp(resolved_path, base.data(), base.length()) != 0) {
5055
return strdup(resolved_path);
5156
}
5257
size_t path_len = strlen(resolved_path);
53-
char* buf(js_pod_malloc<char>(path_len - base_len + 1));
54-
strncpy(buf, &resolved_path[base_len], path_len - base_len + 1);
58+
char* buf(js_pod_malloc<char>(path_len - base.length() + 1));
59+
strncpy(buf, &resolved_path[base.length()], path_len - base.length() + 1);
5560
return buf;
5661
}
5762

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

327332
JS::CompileOptions opts(cx, *COMPILE_OPTS);
328-
opts.setFileAndLine(strip_base(resolved_path, BASE_PATH), 1);
333+
opts.setFileAndLine(strip_prefix(resolved_path, PATH_PREFIX), 1);
329334
return get_module(cx, path.get(), resolved_path, opts);
330335
}
331336

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

352-
ScriptLoader::ScriptLoader(api::Engine* engine, JS::CompileOptions *opts) {
357+
ScriptLoader::ScriptLoader(api::Engine* engine, JS::CompileOptions *opts,
358+
mozilla::Maybe<std::string> path_prefix) {
353359
MOZ_ASSERT(!SCRIPT_LOADER);
354360
ENGINE = engine;
355361
SCRIPT_LOADER = this;
356362
COMPILE_OPTS = opts;
363+
PATH_PREFIX = std::move(path_prefix);
357364
JSContext* cx = engine->cx();
358365
moduleRegistry.init(cx, JS::NewMapObject(cx));
359366
builtinModules.init(cx, JS::NewMapObject(cx));
@@ -452,7 +459,7 @@ bool ScriptLoader::eval_top_level_script(const char *path, JS::SourceText<mozill
452459
JSContext *cx = ENGINE->cx();
453460

454461
JS::CompileOptions opts(cx, *COMPILE_OPTS);
455-
opts.setFileAndLine(strip_base(path, BASE_PATH), 1);
462+
opts.setFileAndLine(strip_prefix(path, PATH_PREFIX), 1);
456463
JS::RootedScript script(cx);
457464
RootedObject module(cx);
458465
if (MODULE_MODE) {

runtime/script_loader.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414

1515
class ScriptLoader {
1616
public:
17-
ScriptLoader(api::Engine* engine, JS::CompileOptions* opts);
17+
ScriptLoader(api::Engine *engine, JS::CompileOptions *opts,
18+
mozilla::Maybe<std::string> path_prefix);
1819
~ScriptLoader();
1920

2021
bool define_builtin_module(const char* id, HandleValue builtin);
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
stderr [0] :: Error while running request handler: runtime error
22
stderr [0] :: Stack:
3-
stderr [0] :: @runtime-err.js:6:13
4-
stderr [0] :: @runtime-err.js:7:7
3+
stderr [0] :: @e2e/runtime-err/runtime-err.js:6:13
4+
stderr [0] :: @e2e/runtime-err/runtime-err.js:7:7
55
stderr [0] ::
66
stderr [0] :: Caused by: error cause
77
stderr [0] :: Stack:
8-
stderr [0] :: @runtime-err.js:6:49
9-
stderr [0] :: @runtime-err.js:7:7
8+
stderr [0] :: @e2e/runtime-err/runtime-err.js:6:49
9+
stderr [0] :: @e2e/runtime-err/runtime-err.js:7:7
1010
stderr [0] ::
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
Exception while evaluating top-level script
2-
syntax-err.js:2:1 SyntaxError: expected expression, got end of script
2+
e2e/syntax-err/syntax-err.js:2:1 SyntaxError: expected expression, got end of script
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
Exception while evaluating top-level script
2-
tla-err.js:3:10 Error: blah
2+
e2e/tla-err/tla-err.js:3:10 Error: blah

tests/test.sh

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,13 @@ if [ -z "$test_component" ]; then
3838
runtime_args="-i $test_dir/init.js $runtime_args"
3939
fi
4040

41+
test_top_level="$(dirname $(dirname "$test_dir"))/"
42+
43+
runtime_args="--strip-path-prefix $test_top_level $runtime_args"
44+
4145
# Run Wizer
4246
set +e
43-
PREOPEN_DIR="$(dirname $(dirname "$test_dir"))" "$test_runtime/componentize.sh" $componentize_flags $runtime_args "$test_component" 1> "$stdout_log" 2> "$stderr_log"
47+
PREOPEN_DIR="$test_top_level" "$test_runtime/componentize.sh" $componentize_flags $runtime_args "$test_component" 1> "$stdout_log" 2> "$stderr_log"
4448
wizer_result=$?
4549
set -e
4650

0 commit comments

Comments
 (0)