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

Added global intrinsic functions in typescript #126

Merged
merged 1 commit into from
May 15, 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
2 changes: 0 additions & 2 deletions nixjs-rt/src/builtins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -356,8 +356,6 @@ export function getBuiltins() {

const pathValue = pathStrict.toJs();

// Below is an intrinsic function that's injected by the Nix evaluator.
// @ts-ignore
const resultingFn: (ctx: EvalCtx) => NixType = importNixModule(pathValue);

const ctx = new EvalCtx(pathValue);
Expand Down
15 changes: 15 additions & 0 deletions nixjs-rt/src/globals.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import type { NixType, EvalCtx } from "./lib";

declare global {
/**
* Import a Nix module from the given path. The path is absolute.
* Returns a transpiled version of the module, executed, with a function
* that takes an EvalCtx and returns the module's value.
*/
var importNixModule: (path: string) => (ctx: EvalCtx) => NixType;

/**
* Log the string provided, purely for debugging purposes.
*/
var debugLog: (log: string) => void;
}
3 changes: 2 additions & 1 deletion nixjs-rt/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"moduleResolution": "Bundler",
"forceConsistentCasingInFileNames": true,
"esModuleInterop": true,
"inlineSourceMap": true
"inlineSourceMap": true,
"typeRoots": ["src/globals.d.ts"]
},
"include": ["src/**/*.ts"]
}
33 changes: 28 additions & 5 deletions src/eval/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,24 @@ pub fn evaluate(nix_expr: &str) -> EvalResult {
let scope = &mut v8::ContextScope::new(scope, context);
let global = context.global(scope);

let import_module_attr = v8::String::new(scope, "importNixModule").unwrap();
let import_module_fn = v8::Function::new(scope, import_nix_module).unwrap();
global
.set(scope, import_module_attr.into(), import_module_fn.into())
.unwrap();
// Insert all globals, as defined by globals.d.ts
let globals: &[(_, v8::Local<v8::Value>)] = &[
(
"importNixModule",
v8::Function::new(scope, import_nix_module).unwrap().into(),
),
(
"debugLog",
v8::Function::new(scope, debug_log).unwrap().into(),
),
];

for (name, value) in globals {
let import_module_attr = v8::String::new(scope, name).unwrap();
global
.set(scope, import_module_attr.into(), *value)
.unwrap();
}

// Execute the Nix runtime JS module, get its exports
let nixjs_rt_str = include_str!("../../nixjs-rt/dist/lib.mjs");
Expand Down Expand Up @@ -110,6 +123,16 @@ fn import_nix_module<'s>(
ret.set(nix_fn.into());
}

fn debug_log<'s>(
scope: &mut HandleScope<'s>,
args: v8::FunctionCallbackArguments<'s>,
_ret: v8::ReturnValue,
) {
// Log the first argument
let log_str = args.get(0).to_rust_string_lossy(scope);
eprintln!("Log from JS: {log_str}");
}

fn exec_module<'a>(
code: &str,
scope: &mut v8::HandleScope<'a>,
Expand Down