/
ffi.js
152 lines (115 loc) · 3.61 KB
/
ffi.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import * as $babel from "babel-core";
import * as $fs from "fs";
import * as $path from "path";
import * as $source_map from "source-map";
import * as $serialize from "./serialize";
import { crash } from "../../builtin/ffi/crash";
import { make_module, lookup_export } from "./state";
import $module from "./babel-plugin-transform-es2015-modules-global";
export const ffi_dir = $path.join("builtin", "ffi");
const resolve1 = (base, path) => {
path = $path.resolve(base, path);
// TODO is this correct ?
if ($path.extname(path) !== ".js") {
path += ".js";
}
return $fs.realpathSync(path);
};
export const resolve = (base, path) => {
const a = /^nulan:(.+)$/["exec"](path);
if (a === null) {
return resolve1(base, path);
} else {
// TODO find relative path
return resolve1(ffi_dir, a[1]);
}
};
const read_file = (path) =>
$fs.readFileSync(path, { "encoding": "utf8" });
export const compile_ffi = (state, path) =>
make_module(state, path, (module) => {
const code = read_file(path);
const dir = $path.dirname(path);
const output = $babel.transform(code, {
"filename": path, // TODO is this correct ?
"sourceMaps": true,
"code": true,
"babelrc": false,
"ast": false,
// sourceMapTarget
// sourceFileName
// sourceRoot
// moduleIds
// moduleId
"presets": [
"es2015-native-modules"
],
"plugins": [
[$module, {
"file": path,
"loadFile": (file) => {
const path = resolve(dir, file);
module.imports["push"](path);
compile_ffi(state, path);
return path;
},
"export": (from, to) => {
// TODO handle conflicts
module.exports[to] = {
module: module.id,
value: from
};
},
"exportFrom": (file, from, to) => {
const _module = state.modules[file];
// TODO handle conflicts
module.exports[to] = lookup_export(_module, from);
},
"exportAllFrom": (file) => {
const _module = state.modules[file];
const exports = _module.exports;
// TODO make this more efficient ?
// TODO is this guaranteed to work in all situations ?
// TODO handle conflicts
for (let name in exports) {
module.exports[name] = exports[name];
}
},
"getAllExports": () => {
const exports = module.exports;
const output = {};
// TODO make this more efficient ?
for (let name in exports) {
const exported = exports[name];
output[name] = $serialize.variable(exported);
}
return output;
},
// TODO
"namespace": "$" + module.id + "$",
"namespaceFrom": (file) => {
const _module = state.modules[file];
// TODO
return "$" + _module.id + "$";
},
"localVariable": (value) =>
// TODO
"$" + module.id + "$" + value,
"importedVariable": (name, file) => {
const _module = state.modules[file];
const exported = lookup_export(_module, name);
return $serialize.variable(exported);
}
}]
]
});
module.output = {
$: 1,
a: $source_map.SourceNode["fromStringWithSourceMap"](
output["code"],
new $source_map.SourceMapConsumer(output["map"]),
// TODO
$path.relative(state.root, dir)
)
};
});