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

support require json file #580

Merged
merged 1 commit into from
Sep 10, 2024
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
14 changes: 14 additions & 0 deletions llrt_core/src/json_loader.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use rquickjs::{loader::Loader, Ctx, Error, Module, Result};

#[derive(Debug)]
pub struct JSONLoader;

impl Loader for JSONLoader {
fn load<'js>(&mut self, ctx: &Ctx<'js>, path: &str) -> Result<Module<'js>> {
if !path.ends_with(".json") {
return Err(Error::new_loading(path));
}
let source = std::fs::read_to_string(path)?;
Module::declare(ctx.clone(), path, ["export default ", &source].concat())
}
}
1 change: 1 addition & 0 deletions llrt_core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ mod compiler_common;
pub mod environment;
pub mod json;
// mod minimal_tracer;
pub mod json_loader;
mod module_builder;
pub mod modules;
pub mod number;
Expand Down
17 changes: 13 additions & 4 deletions llrt_core/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,13 @@ use zstd::{bulk::Decompressor, dict::DecoderDictionary};

include!(concat!(env!("OUT_DIR"), "/bytecode_cache.rs"));

use crate::modules::{
console,
crypto::SYSTEM_RANDOM,
path::{dirname, join_path, resolve_path},
use crate::{
json_loader::JSONLoader,
modules::{
console,
crypto::SYSTEM_RANDOM,
path::{dirname, join_path, resolve_path},
},
};

use crate::{
Expand Down Expand Up @@ -391,6 +394,7 @@ impl Vm {

let loader = LoaderContainer::new((
module_loader,
JSONLoader,
BinaryLoader,
BuiltinLoader::default(),
ScriptLoader::default()
Expand Down Expand Up @@ -605,6 +609,11 @@ fn init(ctx: &Ctx<'_>, module_names: HashSet<&'static str>) -> Result<()> {
join_path(vec![import_directory, specifier])
};

if import_name.ends_with(".json") {
let source = std::fs::read_to_string(import_name)?;
return json_parse(&ctx, source);
}

let mut map = require_in_progress.lock().unwrap();
if let Some(obj) = map.get(&import_name) {
return Ok(obj.clone().into_value());
Expand Down
13 changes: 13 additions & 0 deletions tests/unit/import.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const CWD = process.cwd();

it("should import a js file", async () => {
const mod = await import(`${CWD}/fixtures/hello.js`)

expect(mod.hello).toEqual("hello world!");
});

it("should import a json file", async () => {
const mod = await import(`${CWD}/package.json`)

expect(mod.default.private).toEqual(true);
});
6 changes: 6 additions & 0 deletions tests/unit/require.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,9 @@ it("should be able to use node module with prefix `node:` with require", () => {
consoleObj.error("error");
consoleObj.trace("trace");
});

it("should require json", () => {
const a = _require(`${CWD}/package.json`);

expect(a.private).toEqual(true);
});
Loading