Skip to content

Commit 756fa82

Browse files
authored
chore(module): Refactor module module (#1255)
1 parent 7d749dd commit 756fa82

File tree

5 files changed

+36
-43
lines changed

5 files changed

+36
-43
lines changed

llrt_core/src/modules/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ pub use llrt_modules::{
77
https, module, navigator, net, os, path, perf_hooks, process, stream_web, string_decoder,
88
timers, tls, tty, url, util, zlib,
99
};
10-
pub use llrt_modules::{module_builder, package, require, CJS_IMPORT_PREFIX, CJS_LOADER_PREFIX};
10+
pub use llrt_modules::{module_builder, package, CJS_IMPORT_PREFIX, CJS_LOADER_PREFIX};
1111

1212
#[cfg(feature = "lambda")]
1313
pub mod console;

llrt_modules/src/lib.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0
3+
use std::env;
4+
5+
use once_cell::sync::Lazy;
6+
37
pub mod module;
48
pub mod module_builder;
59
pub mod package;
6-
pub mod require;
710

811
pub use self::modules::*;
912

@@ -72,3 +75,10 @@ pub const CJS_IMPORT_PREFIX: &str = "__cjs:";
7275
pub const CJS_LOADER_PREFIX: &str = "__cjsm:";
7376

7477
pub const ENV_LLRT_PLATFORM: &str = "LLRT_PLATFORM";
78+
79+
pub static LLRT_PLATFORM: Lazy<String> = Lazy::new(|| {
80+
env::var(ENV_LLRT_PLATFORM)
81+
.ok()
82+
.filter(|platform| platform == "node")
83+
.unwrap_or_else(|| "browser".to_string())
84+
});

llrt_modules/src/module/mod.rs

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0
3-
use std::{cell::RefCell, collections::HashSet, marker::PhantomData};
3+
use std::{
4+
cell::RefCell,
5+
collections::{HashMap, HashSet},
6+
marker::PhantomData,
7+
rc::Rc,
8+
};
49

510
use llrt_utils::{
611
ctx::CtxExt,
@@ -15,9 +20,9 @@ use rquickjs::{
1520
};
1621

1722
pub mod loader;
23+
mod require;
1824
pub mod resolver;
1925

20-
use crate::require::{require, RequireState};
2126
use crate::CJS_IMPORT_PREFIX;
2227

2328
#[derive(JsLifetime)]
@@ -39,6 +44,17 @@ impl ModuleNames<'_> {
3944
}
4045
}
4146

47+
#[derive(Default)]
48+
pub struct RequireState<'js> {
49+
pub cache: HashMap<Rc<str>, Value<'js>>,
50+
pub exports: HashMap<Rc<str>, Value<'js>>,
51+
pub progress: HashMap<Rc<str>, Object<'js>>,
52+
}
53+
54+
unsafe impl<'js> JsLifetime<'js> for RequireState<'js> {
55+
type Changed<'to> = RequireState<'to>;
56+
}
57+
4258
#[derive(Clone, JsLifetime)]
4359
struct Hook<'js> {
4460
resolve: Option<Function<'js>>,
@@ -64,13 +80,6 @@ impl ModuleHookState<'_> {
6480

6581
pub struct ModuleModule;
6682

67-
fn create_require(ctx: Ctx<'_>) -> Result<Value<'_>> {
68-
ctx.globals()
69-
.get::<_, Function>("require")
70-
.map(|f| f.into())
71-
.map_err(|_| Exception::throw_reference(&ctx, "create_require is not supported"))
72-
}
73-
7483
fn is_builtin(ctx: Ctx<'_>, name: String) -> Result<bool> {
7584
let module_list = ctx
7685
.userdata::<ModuleNames>()
@@ -111,7 +120,7 @@ impl ModuleDef for ModuleModule {
111120
.map_or_else(HashSet::new, |v| v.get_list());
112121

113122
default.set("builtinModules", module_list)?;
114-
default.set("createRequire", Func::from(create_require))?;
123+
default.set("createRequire", Func::from(require::require))?;
115124
default.set("isBuiltin", Func::from(is_builtin))?;
116125
default.set("registerHooks", Func::from(register_hooks))?;
117126

@@ -170,7 +179,7 @@ pub fn init(ctx: &Ctx) -> Result<()> {
170179
.enumerable();
171180

172181
globals.prop("exports", exports_accessor)?;
173-
globals.set("require", Func::from(require))?;
182+
globals.set("require", Func::from(require::require))?;
174183

175184
let module = Object::new(ctx.clone())?;
176185
module.prop("exports", exports_accessor)?;

llrt_modules/src/require.rs renamed to llrt_modules/src/module/require.rs

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,19 @@
11
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0
3-
use std::{
4-
cell::RefCell,
5-
collections::{HashMap, HashSet},
6-
env, fs,
7-
rc::Rc,
8-
};
3+
use std::{cell::RefCell, collections::HashSet, fs, rc::Rc};
94

105
use llrt_hooking::{invoke_async_hook, register_finalization_registry, HookType};
116
use llrt_json::parse::json_parse;
127
use llrt_utils::{ctx::CtxExt, io::BYTECODE_FILE_EXT, provider::ProviderType};
13-
use once_cell::sync::Lazy;
14-
use rquickjs::{
15-
atom::PredefinedAtom, qjs, Ctx, Filter, Function, JsLifetime, Module, Object, Result, Value,
16-
};
8+
use rquickjs::{atom::PredefinedAtom, qjs, Ctx, Filter, Function, Module, Object, Result, Value};
179
use tokio::time::Instant;
1810
use tracing::trace;
1911

20-
use crate::module::ModuleNames;
2112
use crate::modules::{path::resolve_path, timers::poll_timers};
2213
use crate::package::resolver::require_resolve;
2314
use crate::CJS_IMPORT_PREFIX;
2415

25-
pub static LLRT_PLATFORM: Lazy<String> = Lazy::new(|| {
26-
env::var(super::ENV_LLRT_PLATFORM)
27-
.ok()
28-
.filter(|platform| platform == "node")
29-
.unwrap_or_else(|| "browser".to_string())
30-
});
31-
32-
#[derive(Default)]
33-
pub struct RequireState<'js> {
34-
pub cache: HashMap<Rc<str>, Value<'js>>,
35-
pub exports: HashMap<Rc<str>, Value<'js>>,
36-
pub progress: HashMap<Rc<str>, Object<'js>>,
37-
}
38-
39-
unsafe impl<'js> JsLifetime<'js> for RequireState<'js> {
40-
type Changed<'to> = RequireState<'to>;
41-
}
16+
use super::{ModuleNames, RequireState};
4217

4318
pub fn require(ctx: Ctx<'_>, specifier: String) -> Result<Value<'_>> {
4419
let globals = ctx.globals();

llrt_modules/src/package/resolver.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ use simd_json::{derived::ValueObjectAccessAsScalar, BorrowedValue};
2020
use tracing::trace;
2121

2222
use crate::modules::path;
23-
use crate::require::LLRT_PLATFORM;
24-
use crate::{CJS_IMPORT_PREFIX, CJS_LOADER_PREFIX};
23+
use crate::{CJS_IMPORT_PREFIX, CJS_LOADER_PREFIX, LLRT_PLATFORM};
2524

2625
fn rc_string_to_cow<'a>(rc: Rc<String>) -> Cow<'a, str> {
2726
match Rc::try_unwrap(rc) {

0 commit comments

Comments
 (0)