mirror of
https://github.com/denoland/deno.git
synced 2024-11-25 15:29:32 -05:00
e511022c74
Code run within Deno-mode and Node-mode should have access to a slightly different set of globals. Previously this was done through a compile time code-transform for Node-mode, but this is not ideal and has many edge cases, for example Node's globalThis having a different identity than Deno's globalThis. This commit makes the `globalThis` of the entire runtime a semi-proxy. This proxy returns a different set of globals depending on the caller's mode. This is not a full proxy, because it is shadowed by "real" properties on globalThis. This is done to avoid the overhead of a full proxy for all globalThis operations. The globals between Deno-mode and Node-mode are now properly segregated. This means that code running in Deno-mode will not have access to Node's globals, and vice versa. Deleting a managed global in Deno-mode will NOT delete the corresponding global in Node-mode, and vice versa. --------- Co-authored-by: Bartek Iwańczuk <biwanczuk@gmail.com> Co-authored-by: Aapo Alasuutari <aapo.alasuutari@gmail.com>
94 lines
2.8 KiB
Rust
94 lines
2.8 KiB
Rust
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
|
|
|
use deno_ast::CjsAnalysis;
|
|
use deno_ast::MediaType;
|
|
use deno_ast::ModuleSpecifier;
|
|
use deno_core::error::AnyError;
|
|
use deno_runtime::deno_node::analyze::CjsAnalysis as ExtNodeCjsAnalysis;
|
|
use deno_runtime::deno_node::analyze::CjsCodeAnalyzer;
|
|
use deno_runtime::deno_node::analyze::NodeCodeTranslator;
|
|
|
|
use crate::cache::NodeAnalysisCache;
|
|
use crate::util::fs::canonicalize_path_maybe_not_exists;
|
|
|
|
pub type CliNodeCodeTranslator = NodeCodeTranslator<CliCjsCodeAnalyzer>;
|
|
|
|
/// Resolves a specifier that is pointing into a node_modules folder.
|
|
///
|
|
/// Note: This should be called whenever getting the specifier from
|
|
/// a Module::External(module) reference because that module might
|
|
/// not be fully resolved at the time deno_graph is analyzing it
|
|
/// because the node_modules folder might not exist at that time.
|
|
pub fn resolve_specifier_into_node_modules(
|
|
specifier: &ModuleSpecifier,
|
|
) -> ModuleSpecifier {
|
|
specifier
|
|
.to_file_path()
|
|
.ok()
|
|
// this path might not exist at the time the graph is being created
|
|
// because the node_modules folder might not yet exist
|
|
.and_then(|path| canonicalize_path_maybe_not_exists(&path).ok())
|
|
.and_then(|path| ModuleSpecifier::from_file_path(path).ok())
|
|
.unwrap_or_else(|| specifier.clone())
|
|
}
|
|
|
|
pub struct CliCjsCodeAnalyzer {
|
|
cache: NodeAnalysisCache,
|
|
}
|
|
|
|
impl CliCjsCodeAnalyzer {
|
|
pub fn new(cache: NodeAnalysisCache) -> Self {
|
|
Self { cache }
|
|
}
|
|
|
|
fn inner_cjs_analysis(
|
|
&self,
|
|
specifier: &ModuleSpecifier,
|
|
source: &str,
|
|
) -> Result<CjsAnalysis, AnyError> {
|
|
let source_hash = NodeAnalysisCache::compute_source_hash(source);
|
|
if let Some(analysis) = self
|
|
.cache
|
|
.get_cjs_analysis(specifier.as_str(), &source_hash)
|
|
{
|
|
return Ok(analysis);
|
|
}
|
|
|
|
let media_type = MediaType::from_specifier(specifier);
|
|
if media_type == MediaType::Json {
|
|
return Ok(CjsAnalysis {
|
|
exports: vec![],
|
|
reexports: vec![],
|
|
});
|
|
}
|
|
|
|
let parsed_source = deno_ast::parse_script(deno_ast::ParseParams {
|
|
specifier: specifier.to_string(),
|
|
text_info: deno_ast::SourceTextInfo::new(source.into()),
|
|
media_type,
|
|
capture_tokens: true,
|
|
scope_analysis: false,
|
|
maybe_syntax: None,
|
|
})?;
|
|
let analysis = parsed_source.analyze_cjs();
|
|
self
|
|
.cache
|
|
.set_cjs_analysis(specifier.as_str(), &source_hash, &analysis);
|
|
|
|
Ok(analysis)
|
|
}
|
|
}
|
|
|
|
impl CjsCodeAnalyzer for CliCjsCodeAnalyzer {
|
|
fn analyze_cjs(
|
|
&self,
|
|
specifier: &ModuleSpecifier,
|
|
source: &str,
|
|
) -> Result<ExtNodeCjsAnalysis, AnyError> {
|
|
let analysis = self.inner_cjs_analysis(specifier, source)?;
|
|
Ok(ExtNodeCjsAnalysis {
|
|
exports: analysis.exports,
|
|
reexports: analysis.reexports,
|
|
})
|
|
}
|
|
}
|