summaryrefslogtreecommitdiff
path: root/js/runtime.ts
diff options
context:
space:
mode:
authorRyan Dahl <ry@tinyclouds.org>2018-07-13 03:24:07 -0400
committerRyan Dahl <ry@tinyclouds.org>2018-07-18 15:43:50 -0400
commit3e51605bc9ca98522fc21a0673e690105f48da98 (patch)
tree1c31da16173d95d6623721414b402d1dcdf069b4 /js/runtime.ts
parent8a4e3dfda4975540d6e0059248477facfe03c31c (diff)
Execute JS for the first time in Rust rewrite.
Implements code_fetch handler in Rust. Add ability to embed string assets (for typescript declaration files) Remove deno_cc and deno_cc_nosnapshot targets.
Diffstat (limited to 'js/runtime.ts')
-rw-r--r--js/runtime.ts43
1 files changed, 34 insertions, 9 deletions
diff --git a/js/runtime.ts b/js/runtime.ts
index 9570dda1c..d234cdd85 100644
--- a/js/runtime.ts
+++ b/js/runtime.ts
@@ -11,6 +11,7 @@
import * as ts from "typescript";
import * as util from "./util";
import { log } from "./util";
+import { assetSourceCode } from "./assets";
import * as os from "./os";
//import * as sourceMaps from "./v8_source_maps";
import { window, globalEval } from "./globals";
@@ -22,9 +23,14 @@ const EOL = "\n";
export type AmdFactory = (...args: any[]) => undefined | object;
export type AmdDefine = (deps: string[], factory: AmdFactory) => void;
-/*
// Uncaught exceptions are sent to window.onerror by the privlaged binding.
-window.onerror = (message, source, lineno, colno, error) => {
+window.onerror = (
+ message: string,
+ source: string,
+ lineno: number,
+ colno: number,
+ error: Error
+) => {
// TODO Currently there is a bug in v8_source_maps.ts that causes a segfault
// if it is used within window.onerror. To workaround we uninstall the
// Error.prepareStackTrace handler. Users will get unmapped stack traces on
@@ -33,7 +39,6 @@ window.onerror = (message, source, lineno, colno, error) => {
console.log(error.message, error.stack);
os.exit(1);
};
-*/
/*
export function setup(mainJs: string, mainMap: string): void {
@@ -147,11 +152,31 @@ export function resolveModule(
): null | FileModule {
util.log("resolveModule", { moduleSpecifier, containingFile });
util.assert(moduleSpecifier != null && moduleSpecifier.length > 0);
- // We ask golang to sourceCodeFetch. It will load the sourceCode and if
- // there is any outputCode cached, it will return that as well.
- const fetchResponse = os.codeFetch(moduleSpecifier, containingFile);
- const { filename, sourceCode, outputCode } = fetchResponse;
- if (sourceCode.length === 0) {
+ let filename: string, sourceCode: string, outputCode: string;
+ if (
+ moduleSpecifier.startsWith("/$asset$/") ||
+ containingFile.startsWith("/$asset$/")
+ ) {
+ // Assets are compiled into the runtime javascript bundle.
+ const assetName = moduleSpecifier.split("/").pop();
+ sourceCode = assetSourceCode[assetName];
+ filename = "/$asset$/" + assetName;
+ } else {
+ // We query Rust with a CodeFetch message. It will load the sourceCode, and
+ // if there is any outputCode cached, will return that as well.
+ let fetchResponse;
+ try {
+ fetchResponse = os.codeFetch(moduleSpecifier, containingFile);
+ } catch (e) {
+ // TODO Only catch "no such file or directory" errors. Need error codes.
+ util.log("os.codeFetch error ignored", e.message);
+ return null;
+ }
+ filename = fetchResponse.filename;
+ sourceCode = fetchResponse.sourceCode;
+ outputCode = fetchResponse.outputCode;
+ }
+ if (sourceCode == null || sourceCode.length === 0) {
return null;
}
util.log("resolveModule sourceCode length ", sourceCode.length);
@@ -293,7 +318,7 @@ class TypeScriptHost implements ts.LanguageServiceHost {
}
getDefaultLibFileName(options: ts.CompilerOptions): string {
- const fn = ts.getDefaultLibFileName(options);
+ const fn = "lib.d.ts"; // ts.getDefaultLibFileName(options);
util.log("getDefaultLibFileName", fn);
const m = resolveModule(fn, "/$asset$/");
return m.fileName;