Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/python-wasm
Path: blob/main/python/pylang/tools/compiler.ts
1396 views
1
/*
2
* Copyright (C) 2021 William Stein <[email protected]>
3
* Copyright (C) 2015 Kovid Goyal <kovid at kovidgoyal.net>
4
*
5
* Distributed under terms of the BSD license
6
*/
7
8
// Thin wrapper around (release|dev)/compiler.js to setup some global facilities and
9
// export the compiler's symbols safely.
10
11
import { join, relative } from "path";
12
import { readFileSync as readfile, writeFileSync as writefile } from "fs";
13
import { createContext, runInContext } from "vm";
14
import { sha1sum } from "./utils";
15
16
export type Compiler = any; // for now
17
18
interface Options {
19
console?;
20
}
21
22
export default function createCompiler(options: Options = {}): Compiler {
23
const compiler_exports: Compiler = {};
24
const compiler_context = createContext({
25
console: options.console ?? console,
26
readfile,
27
writefile,
28
sha1sum,
29
require,
30
exports: compiler_exports,
31
});
32
33
const base = join(__dirname, "..", "..");
34
let compiler_dir = join(base, "dist/compiler");
35
const compiler_file = join(compiler_dir, "compiler.js");
36
const compilerjs = readfile(compiler_file, "utf-8");
37
runInContext(compilerjs, compiler_context, relative(base, compiler_file));
38
return compiler_exports;
39
}
40
41