Skip to content

Commit

Permalink
Implement import.meta
Browse files Browse the repository at this point in the history
Closes #26.
  • Loading branch information
TooTallNate committed Oct 13, 2023
1 parent ec79b19 commit 612a208
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/honest-geckos-lie.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'nxjs-runtime': patch
---

Implement `import.meta`
2 changes: 1 addition & 1 deletion apps/tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"type": "module",
"description": "nx.js API tests",
"scripts": {
"build": "esbuild --bundle --target=es2020 src/main.ts --outfile=romfs/main.js",
"build": "esbuild --bundle --target=es2020 --format=esm src/main.ts --outfile=romfs/main.js",
"nro": "nxjs-pack"
},
"license": "MIT",
Expand Down
20 changes: 20 additions & 0 deletions apps/tests/src/import.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { suite } from 'uvu';
import * as assert from 'uvu/assert';

const test = suite('import');

test('`import.meta.url` is a string', () => {
assert.type(import.meta.url, 'string');

// This should match the esbuild bundle output file name
assert.equal(new URL(import.meta.url).href, Switch.entrypoint);
});

test('`import.meta.main` is a boolean', () => {
assert.type(import.meta.main, 'boolean');

// This should be `true` since esbuild is used to bundle
assert.equal(import.meta.main, true);
});

test.run();
1 change: 1 addition & 0 deletions apps/tests/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import './canvas';
import './fetch';
import './form-data';
import './import';
import './switch';
import './wasm';
1 change: 1 addition & 0 deletions apps/tests/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"compilerOptions": {
"target": "ES2020",
"module": "ES2020",
"moduleResolution": "Bundler",
"noEmit": true,
"forceConsistentCasingInFileNames": true,
Expand Down
18 changes: 18 additions & 0 deletions packages/runtime/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,24 @@ import './navigator';
import * as WebAssembly from './wasm';
def('WebAssembly', WebAssembly);

/**
* The `import.meta` meta-property exposes context-specific metadata to a JavaScript module.
* It contains information about the module, such as the module's URL.
*/
export interface ImportMeta {
/**
* Contains the absolute URL of the JavaScript module that is being executed.
*
* @example "romfs:/main.js"
*/
url: string;
/**
* Set to `true` when the JavaScript module that is being executed is the
* entrypoint file of the application.
*/
main: boolean;
}

function touchIsEqual(a: Touch, b: Touch) {
return (
a.screenX === b.screenX &&
Expand Down
29 changes: 28 additions & 1 deletion source/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,23 @@ static JSValue js_get_internal_promise_state(JSContext *ctx, JSValueConst this_v
return arr;
}

int nx_module_set_import_meta(JSContext *ctx, JSValueConst func_val,
const char *url, JS_BOOL is_main)
{
JSModuleDef *m = JS_VALUE_GET_PTR(func_val);
JSValue meta_obj = JS_GetImportMeta(ctx, m);
if (JS_IsException(meta_obj))
return -1;
JS_DefinePropertyValueStr(ctx, meta_obj, "url",
JS_NewString(ctx, url),
JS_PROP_C_W_E);
JS_DefinePropertyValueStr(ctx, meta_obj, "main",
JS_NewBool(ctx, is_main),
JS_PROP_C_W_E);
JS_FreeValue(ctx, meta_obj);
return 0;
}

void nx_process_pending_jobs(JSRuntime *rt)
{
JSContext *ctx;
Expand Down Expand Up @@ -578,12 +595,22 @@ int main(int argc, char *argv[])
JS_SetPropertyStr(ctx, switch_obj, "argv", argv_array);

// Run the user code
JSValue user_code_result = JS_Eval(ctx, user_code, user_code_size, js_path, JS_EVAL_TYPE_GLOBAL);
JSValue user_code_result = JS_Eval(ctx, user_code, user_code_size, js_path, JS_EVAL_TYPE_MODULE | JS_EVAL_FLAG_COMPILE_ONLY);
if (JS_IsException(user_code_result))
{
print_js_error(ctx);
had_error = 1;
}
else
{
nx_module_set_import_meta(ctx, user_code_result, js_path, true);
user_code_result = JS_EvalFunction(ctx, user_code_result);
if (JS_IsException(user_code_result))
{
print_js_error(ctx);
had_error = 1;
}
}
JS_FreeValue(ctx, user_code_result);
free(user_code);
if (js_path_needs_free)
Expand Down

0 comments on commit 612a208

Please sign in to comment.