Repository: awslabs/llrt
File: llrt_core/src/vm.rs (Rust)

Code before:
            exports.as_ref().unwrap()
        } else {
            exports.replace(Object::new(ctx.clone())?.into_value());
            exports.as_ref().unwrap()
        };
        exports.as_object().unwrap().set(prop, value)?;
        Result::Ok(true)
    }))?;

    globals.prop("exports", exports_proxy)?;

    globals.set("__bootstrap", Object::new(ctx.clone())?)?;

    globals.set(
        "require",
        Func::from(move |ctx, specifier: String| -> Result<Value> {
            struct Args<'js>(Ctx<'js>);
            let Args(ctx) = Args(ctx);
            let specifier = if let Some(striped_specifier) = &specifier.strip_prefix("node:") {
                striped_specifier.to_string()
            } else {
                specifier
            };
            let import_name = if module_names.contains(specifier.as_str())
                || BYTECODE_CACHE.contains_key(&specifier)
                || Path::new(&specifier).is_absolute()
            {
                specifier
            } else {
                let module_name = get_script_or_module_name(ctx.clone());
                let abs_path = resolve_path([module_name].iter());
                let import_directory = dirname(abs_path);
                let ads_specifier = if specifier.ends_with(".js")
                    || specifier.ends_with(".mjs")
                    || specifier.ends_with(".cjs")
                {
                    specifier
                } else if specifier.starts_with("./") || specifier.starts_with("../") {
                    [&import_directory, "/", &specifier].concat()
                } else {
                    get_module_path_in_node_modules(&ctx, &specifier)?
                };
                resolve_path([import_directory, ads_specifier].iter())
            };

            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();

Review comment (performance):
We have a special `is_absolute` method in llrt path module. This is a bit more optimized than Path from rust

Code after:
            exports.as_ref().unwrap()
        } else {
            exports.replace(Object::new(ctx.clone())?.into_value());
            exports.as_ref().unwrap()
        };
        exports.as_object().unwrap().set(prop, value)?;
        Result::Ok(true)
    }))?;

    globals.prop("exports", exports_proxy)?;

    globals.set("__bootstrap", Object::new(ctx.clone())?)?;

    globals.set(
        "require",
        Func::from(move |ctx, specifier: String| -> Result<Value> {
            struct Args<'js>(Ctx<'js>);
            let Args(ctx) = Args(ctx);
            let specifier = if let Some(striped_specifier) = &specifier.strip_prefix("node:") {
                striped_specifier.to_string()
            } else {
                specifier
            };
            let import_name = if module_names.contains(specifier.as_str())
                || BYTECODE_CACHE.contains_key(&specifier)
                || is_absolute(&specifier)
            {
                specifier
            } else {
                let module_name = get_script_or_module_name(ctx.clone());
                let abs_path = resolve_path([module_name].iter());
                let import_directory = dirname(abs_path);
                let ads_specifier = if specifier.ends_with(".js")
                    || specifier.ends_with(".mjs")
                    || specifier.ends_with(".cjs")
                {
                    specifier
                } else if specifier.starts_with("./") || specifier.starts_with("../") {
                    [&import_directory, "/", &specifier].concat()
                } else {
                    get_module_path_in_node_modules(&ctx, &specifier)?
                };
                resolve_path([import_directory, ads_specifier].iter())
            };

            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();