open
https://gitlab.synchro.net/main/sbbs/-/work_items/1372
jsdoor is the only Synchronet JS host that sets `JSOPTION_VAROBJFIX` (`src/sbbs3/jsexec.cpp`, the `#ifdef JSDOOR` `js_opts`), enabled as an experiment in 96f3df2d0c (yellow-3-trades, 2021-03-07). jsexec and the servers use `JAVASCRIPT_OPTIONS` (0x10, COMPILE_N_GO only).
One consequence: `require()` fails inside any non-global scope under jsdoor. With VAROBJFIX, a loaded file's top-level `function`/`var` declarations bind to the global object. But `js_require()` (`src/sbbs3/js_global.cpp`) checks for the symbol with `JS_HasProperty()` on `JS_GetScopeChain(cx)` only, which inside `js.exec(file, scope, obj)` or `load(scope, file)` is the scope object, not the global. So under jsdoor:
- `require(path, "sym")` loads the file and then fails with `symbol 'sym' not defined by script '...'`, although `typeof sym` is `"function"` right afterwards.
- `require()` never sees a symbol that was already loaded, so it always searches for the file again.
a563651f4e (graduation-38-grab, 2019-08-29) turned VAROBJFIX off for this very reason ("it causes variables to be created in the global scope when a scope is passed to load(), require() or js.exec()"). 96f3df2d0c turned it back on for jsdoor only.
### How it showed up
The `jsdoor-testsuite` CI jobs have failed on every platform since pipeline 13032: `exec/tests/listfiles/lib.js` fails with `listfiles_lib.js line 8 Error: can't open attr_option.js`. `exec/tests/test.js` runs each test with `js.exec(testscript, location, new function(){})`, so the library's bare `require("attr_option.js", "attr_option")` runs in a non-global scope. jsdoor also has no configured exec dir (its `scfg` is never loaded), so the bare filename can't be resolved against `exec/load/` either. Loading `attr_option.js` by path in the test first does not help, for the reason above.
The test works around it for now by copying the function onto its own scope object (`this.attr_option = attr_option`) before loading the library.
### Repro (jsdoor built from master, run from a tree with `ctrl/` and `exec/`)
`exec/tests/zprobe/p.js`:
```js
require(js.exec_dir + "../../load/attr_option.js", "attr_option"); log("typeof=" + typeof attr_option);
```
```
SBBSCTRL=./ctrl jsdoor -S -f exec/tests/test.js zprobe
p.js line 1 Error: symbol 'attr_option' not defined by script '.../exec/load/attr_option.js'
```
The same probe passes under jsexec. The same `require()` at a script's top level (global scope) passes under jsdoor too.
### Question
Is VAROBJFIX still wanted in jsdoor? If yes, `js_require()` probably needs to look the symbol up where VAROBJFIX puts it (the global object, or along the scope chain), both before and after the load. If not, removing it brings jsdoor back in line with jsexec.
Separately: should jsdoor resolve bare `load()`/`require()` names against an exec dir (e.g. one next to the script, or one named on the command line), so that libraries in `exec/load/` that `require()` other libraries work under jsdoor?
-- *Authored by Claude (Claude Code), on behalf of @rswindell*
--- SBBSecho 3.38-Linux
* Origin: Vertrauen - [vert/cvs/bbs].synchro.net (1:103/705)