fmt: tidy
This commit is contained in:
@@ -22,7 +22,8 @@ impl<Ctx: CodegenContext> Compile<Ctx> for Ir {
|
||||
},
|
||||
Ir::Str(s) => {
|
||||
// Escape string for JavaScript
|
||||
let escaped = s.val
|
||||
let escaped = s
|
||||
.val
|
||||
.replace('\\', "\\\\")
|
||||
.replace('"', "\\\"")
|
||||
.replace('\n', "\\n")
|
||||
@@ -295,11 +296,17 @@ impl<Ctx: CodegenContext> Compile<Ctx> for HasAttr {
|
||||
match attr {
|
||||
Attr::Str(sym) => {
|
||||
let key = ctx.get_sym(*sym);
|
||||
current = format!("(Nix.force({}) !== null && Nix.force({}) !== undefined && \"{}\" in Nix.force({}))", current, current, key, current);
|
||||
current = format!(
|
||||
"(Nix.force({}) !== null && Nix.force({}) !== undefined && \"{}\" in Nix.force({}))",
|
||||
current, current, key, current
|
||||
);
|
||||
}
|
||||
Attr::Dynamic(expr_id) => {
|
||||
let key = ctx.get_ir(*expr_id).compile(ctx);
|
||||
current = format!("(Nix.force({}) !== null && Nix.force({}) !== undefined && Nix.force({}) in Nix.force({}))", current, current, key, current);
|
||||
current = format!(
|
||||
"(Nix.force({}) !== null && Nix.force({}) !== undefined && Nix.force({}) in Nix.force({}))",
|
||||
current, current, key, current
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -57,7 +57,9 @@ impl<Ctx: DowngradeContext> Downgrade<Ctx> for ast::Path {
|
||||
fn downgrade(self, ctx: &mut Ctx) -> Result<ExprId> {
|
||||
// Collect all parts and check if there are any interpolations
|
||||
let parts_ast: Vec<_> = self.parts().collect();
|
||||
let has_interpolation = parts_ast.iter().any(|part| matches!(part, ast::InterpolPart::Interpolation(_)));
|
||||
let has_interpolation = parts_ast
|
||||
.iter()
|
||||
.any(|part| matches!(part, ast::InterpolPart::Interpolation(_)));
|
||||
|
||||
let parts = if !has_interpolation {
|
||||
// Pure literal path - resolve at compile time
|
||||
@@ -87,20 +89,18 @@ impl<Ctx: DowngradeContext> Downgrade<Ctx> for ast::Path {
|
||||
current_dir
|
||||
.join(&path_str)
|
||||
.canonicalize()
|
||||
.map_err(|e| crate::error::Error::downgrade_error(
|
||||
format!("Failed to resolve path {}: {}", path_str, e)
|
||||
))?
|
||||
.map_err(|e| {
|
||||
crate::error::Error::downgrade_error(format!(
|
||||
"Failed to resolve path {}: {}",
|
||||
path_str, e
|
||||
))
|
||||
})?
|
||||
.to_string_lossy()
|
||||
.to_string()
|
||||
};
|
||||
|
||||
// Return single string part with resolved path
|
||||
vec![ctx.new_expr(
|
||||
Str {
|
||||
val: resolved_path,
|
||||
}
|
||||
.to_ir(),
|
||||
)]
|
||||
vec![ctx.new_expr(Str { val: resolved_path }.to_ir())]
|
||||
} else {
|
||||
// Path with interpolation - do NOT resolve at compile time
|
||||
// Keep literal parts as-is and defer resolution to runtime
|
||||
|
||||
@@ -111,9 +111,7 @@ fn op_import(#[string] path: String) -> std::result::Result<String, NixError> {
|
||||
CONTEXT_HOLDER.with(|holder| {
|
||||
let mut ptr = holder
|
||||
.borrow()
|
||||
.ok_or_else(|| -> NixError {
|
||||
"No context available".to_string().into()
|
||||
})?;
|
||||
.ok_or_else(|| -> NixError { "No context available".to_string().into() })?;
|
||||
let ctx = unsafe { ptr.as_mut() };
|
||||
|
||||
// 1. Resolve path relative to current file (or CWD if top-level)
|
||||
@@ -137,8 +135,7 @@ fn op_import(#[string] path: String) -> std::result::Result<String, NixError> {
|
||||
let _guard = ImportPathGuard::push(absolute_path.clone());
|
||||
|
||||
// 3. Read file
|
||||
let content = std::fs::read_to_string(&absolute_path)
|
||||
.map_err(|e| -> NixError {
|
||||
let content = std::fs::read_to_string(&absolute_path).map_err(|e| -> NixError {
|
||||
format!("Failed to read {}: {}", absolute_path.display(), e).into()
|
||||
})?;
|
||||
|
||||
@@ -149,22 +146,19 @@ fn op_import(#[string] path: String) -> std::result::Result<String, NixError> {
|
||||
"Parse error in {}: {:?}",
|
||||
absolute_path.display(),
|
||||
root.errors()
|
||||
).into());
|
||||
)
|
||||
.into());
|
||||
}
|
||||
|
||||
// 5. Downgrade to IR
|
||||
let expr = root
|
||||
.tree()
|
||||
.expr()
|
||||
.ok_or_else(|| -> NixError {
|
||||
"No expression in file".to_string().into()
|
||||
})?;
|
||||
.ok_or_else(|| -> NixError { "No expression in file".to_string().into() })?;
|
||||
let expr_id = ctx
|
||||
.downgrade_ctx()
|
||||
.downgrade(expr)
|
||||
.map_err(|e| -> NixError {
|
||||
format!("Downgrade error: {}", e).into()
|
||||
})?;
|
||||
.map_err(|e| -> NixError { format!("Downgrade error: {}", e).into() })?;
|
||||
|
||||
// 6. Codegen - returns JS code string
|
||||
Ok(ctx.get_ir(expr_id).compile(ctx))
|
||||
@@ -175,9 +169,7 @@ fn op_import(#[string] path: String) -> std::result::Result<String, NixError> {
|
||||
#[string]
|
||||
fn op_read_file(#[string] path: String) -> std::result::Result<String, NixError> {
|
||||
std::fs::read_to_string(&path)
|
||||
.map_err(|e| -> NixError {
|
||||
format!("Failed to read {}: {}", path, e).into()
|
||||
})
|
||||
.map_err(|e| -> NixError { format!("Failed to read {}: {}", path, e).into() })
|
||||
}
|
||||
|
||||
#[deno_core::op2(fast)]
|
||||
@@ -207,9 +199,7 @@ fn op_resolve_path(#[string] path: String) -> std::result::Result<String, NixErr
|
||||
.join(&path)
|
||||
.canonicalize()
|
||||
.map(|p| p.to_string_lossy().to_string())
|
||||
.map_err(|e| -> NixError {
|
||||
format!("Failed to resolve path {}: {}", path, e).into()
|
||||
})
|
||||
.map_err(|e| -> NixError { format!("Failed to resolve path {}: {}", path, e).into() })
|
||||
}
|
||||
|
||||
// Runtime context for V8 value conversion
|
||||
@@ -271,7 +261,10 @@ pub fn run(script: String, ctx: &mut Context) -> Result<Value> {
|
||||
|
||||
// Initialize V8 once
|
||||
INIT.call_once(|| {
|
||||
JsRuntime::init_platform(Some(v8::new_default_platform(0, false).make_shared()), false);
|
||||
JsRuntime::init_platform(
|
||||
Some(v8::new_default_platform(0, false).make_shared()),
|
||||
false,
|
||||
);
|
||||
});
|
||||
|
||||
// Create a new JsRuntime for each evaluation to avoid state issues
|
||||
@@ -439,7 +432,8 @@ fn to_value_working() {
|
||||
run(
|
||||
"({
|
||||
test: [1., 9223372036854775807n, true, false, 'hello world!']
|
||||
})".into(),
|
||||
})"
|
||||
.into(),
|
||||
&mut ctx
|
||||
)
|
||||
.unwrap(),
|
||||
|
||||
Reference in New Issue
Block a user