Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Zinnia.versions.{zinnia,v8} #261

Merged
merged 3 commits into from
Jun 23, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
18 changes: 11 additions & 7 deletions cli/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,17 @@ async fn main_impl() -> Result<()> {
.context("cannot initialize the IPFS retrieval client Lassie")?,
);

let config = BootstrapOptions::new(
format!("zinnia/{}", env!("CARGO_PKG_VERSION")),
Rc::new(ConsoleReporter::new(Duration::from_millis(500))),
lassie_daemon,
None,
);
run_js_module(&main_module, &config).await?;
let runtime_config = BootstrapOptions {
zinnia_version: env!("CARGO_PKG_VERSION"),
..BootstrapOptions::new(
format!("zinnia/{}", env!("CARGO_PKG_VERSION")),
Rc::new(ConsoleReporter::new(Duration::from_millis(500))),
lassie_daemon,
None,
)
};

run_js_module(&main_module, &runtime_config).await?;
Ok(())
}
}
Expand Down
5 changes: 3 additions & 2 deletions daemon/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ async fn run(config: CliArgs) -> Result<()> {
)?;
let module_root = get_module_root(&main_module)?;

let config = BootstrapOptions {
let runtime_config = BootstrapOptions {
zinnia_version: env!("CARGO_PKG_VERSION"),
agent_version: format!("zinniad/{} {module_name}", env!("CARGO_PKG_VERSION")),
wallet_address: config.wallet_address,
reporter: Rc::new(StationReporter::new(
Expand All @@ -86,7 +87,7 @@ async fn run(config: CliArgs) -> Result<()> {
// TODO: handle module exit and restart it
// https://github.com/filecoin-station/zinnia/issues/146
log::info!("Starting module {main_module}");
run_js_module(&main_module, &config).await?;
run_js_module(&main_module, &runtime_config).await?;

Ok(())
}
Expand Down
11 changes: 11 additions & 0 deletions docs/building-modules.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ import * as code from "../../other/code.js";
- [libp2p](#libp2p)
- [Integration with Filecoin Station](#integration-with-filecoin-station)
- [IPFS retrieval client](#ipfs-retrieval-client)
- [Miscelaneous APIs]()

### Standard JavaScript APIs

Expand Down Expand Up @@ -361,6 +362,16 @@ possible error status codes in
The format of CAR data returned by the retrieval client is described in
[Lassie's Returned CAR Specification](https://github.com/filecoin-project/lassie/blob/main/docs/CAR.md).

### Miscelaneous APIs

#### `Zinnia.versions.zinna`

The version of Zinnia runtime, e.g. `"0.11.0"`.

#### `Zinnia.versions.v8`

The version of V8 engine, e.g. `"11.5.150.2"`.

## Testing Guide

Zinnia provides lightweight tooling for writing and running automated tests.
Expand Down
17 changes: 15 additions & 2 deletions runtime/js/90_zinnia_apis.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
const primordials = globalThis.__bootstrap.primordials;
const { ObjectDefineProperties, ObjectCreate } = primordials;
const { ObjectDefineProperties, ObjectCreate, ObjectFreeze } = primordials;

const { ops } = globalThis.Deno.core;

import { readOnly } from "ext:zinnia_runtime/06_util.js";
import * as libp2p from "ext:zinnia_libp2p/01_peer.js";

const versions = {
zinnia: "",
v8: "",
};

function setVersions(zinniaVersion, v8Version) {
versions.zinnia = zinniaVersion;
versions.v8 = v8Version;

ObjectFreeze(versions);
}

const zinniaNs = ObjectCreate(null);
ObjectDefineProperties(zinniaNs, libp2p.defaultPeerProps);

Expand All @@ -18,6 +30,7 @@ ObjectDefineProperties(activityApi, {
ObjectDefineProperties(zinniaNs, {
activity: readOnly(activityApi),
jobCompleted: readOnly(reportJobCompleted),
versions: readOnly(versions),
});

function reportInfoActivity(msg) {
Expand All @@ -39,4 +52,4 @@ function log(msg, level) {
ops.op_zinnia_log(msg, level);
}

export { zinniaNs, log };
export { zinniaNs, log, setVersions };
2 changes: 2 additions & 0 deletions runtime/js/99_main.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import {
windowOrWorkerGlobalScope,
} from "ext:zinnia_runtime/98_global_scope.js";
import { setLassieUrl } from "ext:zinnia_runtime/fetch.js";
import { setVersions } from "ext:zinnia_runtime/90_zinnia_apis.js";

function formatException(error) {
if (ObjectPrototypeIsPrototypeOf(ErrorPrototype, error)) {
Expand Down Expand Up @@ -69,6 +70,7 @@ function runtimeStart(runtimeOptions) {
Error.prepareStackTrace = core.prepareStackTrace;

setLassieUrl(runtimeOptions.lassieUrl);
setVersions(runtimeOptions.zinniaVersion, runtimeOptions.v8Version);
}

let hasBootstrapped = false;
Expand Down
7 changes: 7 additions & 0 deletions runtime/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ pub struct BootstrapOptions {
/// Lassie daemon to use as the IPFS retrieval client. We must use Arc here to allow sharing of
/// the singleton Lassie instance between multiple threads spawned by Rust's test runner.
pub lassie_daemon: Arc<lassie::Daemon>,

/// Zinnia version reported by `Zinnia.versions.zinnia` API.
/// Embedders can customize this value.
pub zinnia_version: &'static str,
}

impl BootstrapOptions {
Expand All @@ -59,6 +63,7 @@ impl BootstrapOptions {
wallet_address: String::from("t1abjxfbp274xpdqcpuaykwkfb43omjotacm2p3za"),
reporter,
lassie_daemon,
zinnia_version: env!("CARGO_PKG_VERSION"),
bajtos marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand All @@ -68,6 +73,8 @@ impl BootstrapOptions {
"isTty": self.is_tty,
"walletAddress": self.wallet_address,
"lassieUrl": format!("http://127.0.0.1:{}/", self.lassie_daemon.port()),
"zinniaVersion": self.zinnia_version,
"v8Version": deno_core::v8_version(),
});
serde_json::to_string_pretty(&payload).unwrap()
}
Expand Down
19 changes: 19 additions & 0 deletions runtime/tests/js/versions_tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { test } from "zinnia:test";
import { assertMatch, assertEquals, assertArrayIncludes } from "zinnia:assert";

console.log(Zinnia.versions);

test("Zinnia.versions", () => {
assertArrayIncludes(Object.keys(Zinnia), ["versions"], "Zinnia properties");
assertEquals(typeof Zinnia.versions, "object", "typeof Zinnia.versions");
});

test("Zinnia.versions.zinnia", () => {
assertArrayIncludes(Object.keys(Zinnia.versions), ["zinnia"], "Zinnia.versions properties");
assertMatch(Zinnia.versions?.zinnia, /^\d+\.\d+\.\d+$/);
bajtos marked this conversation as resolved.
Show resolved Hide resolved
});

test("Zinnia.versions.v8", () => {
assertArrayIncludes(Object.keys(Zinnia.versions), ["v8"], "Zinnia.versions properties");
assertMatch(Zinnia.versions.v8, /^\d+\.\d+\.\d+\.\d+$/);
});
1 change: 1 addition & 0 deletions runtime/tests/runtime_integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ macro_rules! test_runner_tests(
);

js_tests!(globals_tests);
js_tests!(versions_tests);
js_tests!(timers_tests);
js_tests!(webapis_tests);
js_tests!(webcrypto_tests);
Expand Down