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
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ form_urlencoded = "1.2"
futures-channel = "0.3"
futures-lite = "2"
futures-util = "0.3"
heck = "0.5"
http = "1"
http-body = "1"
http-body-util = "0.1"
Expand Down
24 changes: 20 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ console.log(greet("world"));
starlingmonkey main.js
```

JSON modules are imported with an import attribute, and `import()` loads a module
dynamically:

```js
import config from "./config.json" with { type: "json" };
const { greet } = await import("./greet.js");
```

For quick one-liners, use `-e`:

```bash
Expand Down Expand Up @@ -141,9 +149,17 @@ is `undefined` at the top level.
**Legacy script mode** (`--legacy-script`) — sloppy mode, no
`import`/`export`, `this` is the global object.

**Exit status** — `0` when the script and all the work it scheduled complete, `1`
when the script throws or an initializer, the event loop or the runtime fails (the
error is printed to stderr), and `2` for invalid command-line arguments.
**Output** — `console.log`, `console.info` and `console.debug` print to stdout,
and `console.warn` and `console.error` print to stderr with a `Warn:` or `Error:`
prefix. An exception a timer or other task throws is reported on stderr as
`Uncaught` with its message and stack, and a promise rejected with no handler as
`Uncaught (in promise)` with the reason's.

**Exit status** — `0` when the script and all the work it scheduled complete,
including when a promise rejected with no handler. `1` when the script throws,
its top-level `await` rejects or never settles, or an initializer, the event loop
or the runtime fails, with the error printed to stderr. `2` for invalid
command-line arguments.

---

Expand Down Expand Up @@ -608,7 +624,7 @@ impl Fetcher {
```

The method returns the `Promise` to JS immediately, and the future is queued on
the event loop. `core_runtime::event_loop::run_to_completion`, which
the event loop. `ScriptEvaluation::run_to_completion`, which
`libstarling::run` drives for you, polls it and settles the `Promise` with the
future's `Ok`/`Err`. Two other constructors cover the cases `new` doesn't:
`PromiseFuture::new_void` for futures resolving to `()`, and `PromiseFuture::from_value`
Expand Down
53 changes: 22 additions & 31 deletions crates/builtins/cpp-builtins/cpp/builtins/console.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -405,33 +405,24 @@ JS::Result<mozilla::Ok> ToSource(JSContext *cx, std::string &sourceOut, JS::Hand

namespace builtins::web::console {

// `log`, `info` and `debug` print the bare message on stdout. `warn` and `error` print it on
// stderr, prefixed with the level.
__attribute__((weak))
void builtin_impl_console_log(Console::LogType log_ty, const char *msg) {
const char *prefix = nullptr;
switch (log_ty) {
case Console::LogType::Log:
prefix = "Log";
break;
case Console::LogType::Debug:
prefix = "Debug";
break;
case Console::LogType::Info:
prefix = "Info";
break;
case Console::LogType::Warn:
prefix = "Warn";
break;
fprintf(stderr, "Warn: %s\n", msg);
fflush(stderr);
return;
case Console::LogType::Error:
prefix = "Error";
break;
fprintf(stderr, "Error: %s\n", msg);
fflush(stderr);
return;
default:
prefix = "";
break;
fprintf(stdout, "%s\n", msg);
fflush(stdout);
return;
}
MOZ_ASSERT(prefix);

fprintf(stdout, "%s: %s\n", prefix, msg);
fflush(stdout);
}

template <Console::LogType log_ty>
Expand Down Expand Up @@ -470,9 +461,9 @@ static bool console_out(JSContext *cx, unsigned argc, JS::Value *vp) {
static bool assert_(JSContext *cx, unsigned argc, JS::Value *vp) {
JS::CallArgs args = CallArgsFromVp(argc, vp);
args.rval().setUndefined();
auto condition = args.get(0).toBoolean();
bool condition = JS::ToBoolean(args.get(0));
// 1. If condition is true, return.
if (!condition) {
if (condition) {
return true;
}

Expand All @@ -486,27 +477,27 @@ static bool assert_(JSContext *cx, unsigned argc, JS::Value *vp) {
// 4.3. Otherwise:
// 4.3.1. Let concat be the concatenation of message, U+003A (:), U+0020
// SPACE, and first. 4.3.2. Set data[0] to concat.
// The data are the arguments after the condition. A string first datum is
// joined to the message with ": ", and any other one follows it after a space.
auto length = args.length();
if (length > 1) {
message += ": ";
if (args.get(1).isString()) {
message += ":";
}
JS::RootedObjectVector visitedObjects(cx);
for (int i = 0; i < length; i++) {
for (unsigned i = 1; i < length; i++) {
JS::HandleValue arg = args.get(i);
std::string source;
auto result = ToSource(cx, source, arg, &visitedObjects);
if (result.isErr()) {
return false;
}
// strip quotes for direct string logs
if (source[0] == '"' && source[source.length() - 1] == '"') {
if (source.length() >= 2 && source[0] == '"' && source[source.length() - 1] == '"') {
source = source.substr(1, source.length() - 2);
}
if (!message.empty()) {
message += " ";
message += source;
} else {
message += source;
}
message += " ";
message += source;
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/builtins/web-streams/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ edition.workspace = true
test = false

[dependencies]
smallvec = "1.16"
smallvec.workspace = true
core-runtime.workspace = true
js.workspace = true
web-globals.workspace = true
Expand Down
4 changes: 3 additions & 1 deletion crates/core-runtime/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,9 @@ pub struct RuntimeConfig {
#[command(flatten)]
pub serve_limits: ServeLimits,

/// Pre-initialize the runtime (used during wizer snapshot).
/// Pre-initialize the runtime for a Wizer snapshot. The runtime then records where each piece
/// of asynchronous work is created, for reporting work still pending when the snapshot is
/// taken (see [`EventLoop::ensure_idle_for_snapshot`](crate::event_loop::EventLoop::ensure_idle_for_snapshot)).
#[arg(skip)]
pub pre_initialize: bool,
}
Expand Down
Loading
Loading