@suchipi/quickjs 0.4.0 → 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,180 @@
1
+ /**
2
+ * A global which lets you configure the module loader (import/export/require).
3
+ * You can use these properties to add support for importing new filetypes.
4
+ *
5
+ * This global can also be used to identify whether an object is a module
6
+ * namespace record.
7
+ */
8
+ interface ModuleGlobal {
9
+ /**
10
+ * Returns true if `target` is a module namespace object.
11
+ */
12
+ [Symbol.hasInstance](target: any): target is {
13
+ [key: string | number | symbol]: any;
14
+ };
15
+
16
+ /**
17
+ * A list of filetype extensions that may be omitted from an import specifier
18
+ * string.
19
+ *
20
+ * Defaults to `[".js"]`. You can add more strings to this array to
21
+ * make the engine search for additional files when resolving a
22
+ * require/import.
23
+ *
24
+ * See the doc comment on {@link require} for more information.
25
+ *
26
+ * NOTE: If you add a new extension to this array, you will likely also want
27
+ * to add to {@link Module.compilers}.
28
+ */
29
+ searchExtensions: Array<string>;
30
+
31
+ /**
32
+ * User-defined functions which will handle getting the JavaScript code
33
+ * associated with a module.
34
+ *
35
+ * The key for each property in this object should be a file extension
36
+ * string with a leading dot, eg `".jsx"`. The value for each property should
37
+ * be a function which receives (1) the filepath to a module, and (2) that
38
+ * file's content as a UTF-8 string, and the function should return a string
39
+ * containing JavaScript code that corresponds to that module. In most cases,
40
+ * these functions will compile the contents of the file from one format into JavaScript.
41
+ *
42
+ * The function does not have to use the second 'content' argument it
43
+ * receives (ie. when loading binary files).
44
+ *
45
+ * By adding to this object, you can make it possible to import non-js
46
+ * filetypes; compile-to-JS languages like JSX, TypeScript, and CoffeeScript
47
+ * can be compiled at import time, and asset files like .txt files or .png
48
+ * files can be converted into an appropriate data structure at import time.
49
+ *
50
+ * As an example, to make it possible to import .txt files, you might do:
51
+ * ```js
52
+ * import * as std from "std";
53
+ *
54
+ * Module.compilers[".txt"] = (filename, content) => {
55
+ * return `export default ${JSON.stringify(content)}`;
56
+ * }
57
+ * ```
58
+ * (leveraging `JSON.stringify`'s ability to escape quotes).
59
+ *
60
+ * Then, later in your code, you can do:
61
+ * ```js
62
+ * import names from "./names.txt";
63
+ * ```
64
+ *
65
+ * And `names` will be a string containing the contents of names.txt.
66
+ *
67
+ * NOTE: When adding to this object, you may also wish to add to
68
+ * {@link Module.searchExtensions}.
69
+ */
70
+ compilers: {
71
+ [extensionWithDot: string]: (filename: string, content: string) => string;
72
+ };
73
+
74
+ /**
75
+ * Create a virtual built-in module whose exports consist of the own
76
+ * enumerable properties of `obj`.
77
+ */
78
+ define(name: string, obj: { [key: string]: any }): void;
79
+
80
+ /**
81
+ * Resolves a require/import request from `fromFile` into a canonicalized path.
82
+ *
83
+ * To change native module resolution behavior, replace this function with
84
+ * your own implementation. Note that you must handle
85
+ * `Module.searchExtensions` yourself in your replacement implementation.
86
+ */
87
+ resolve(name: string, fromFile: string): string;
88
+
89
+ /**
90
+ * Reads the contents of the given resolved module name into a string.
91
+ *
92
+ * To change native module loading behavior, replace this function with your
93
+ * own implementation. Note that you must handle `Module.compilers` yourself
94
+ * in your replacement implementation.
95
+ */
96
+ read(modulePath: string): string;
97
+ }
98
+
99
+ // global added by QJMS_AddModuleGlobal
100
+ declare var Module: ModuleGlobal;
101
+
102
+ interface RequireFunction {
103
+ /**
104
+ * Synchronously import a module.
105
+ *
106
+ * `source` will be resolved relative to the calling file.
107
+ *
108
+ * If `source` does not have a file extension, and a file without an extension
109
+ * cannot be found, the engine will check for files with the extensions in
110
+ * {@link Module.searchExtensions}, and use one of those if present. This
111
+ * behavior also happens when using normal `import` statements.
112
+ *
113
+ * For example, if you write:
114
+ *
115
+ * ```js
116
+ * import something from "./somewhere";
117
+ * ```
118
+ *
119
+ * but there's no file named `somewhere` in the same directory as the file
120
+ * where that import appears, and `Module.searchExtensions` is the default
121
+ * value:
122
+ *
123
+ * ```js
124
+ * [".js"]
125
+ * ```
126
+ *
127
+ * then the engine will look for `somewhere.js`. If that doesn't exist, the
128
+ * engine will look for `somewhere/index.js`. If *that* doesn't exist, an error
129
+ * will be thrown.
130
+ *
131
+ * If you add more extensions to `Module.searchExtensions`, then the engine
132
+ * will use those, too. It will search in the same order as the strings appear
133
+ * in the `Module.searchExtensions` array.
134
+ */
135
+ (source: string): any;
136
+
137
+ /**
138
+ * Resolves the normalized path to a modules, relative to the calling file.
139
+ */
140
+ resolve: (source: string) => string;
141
+ }
142
+
143
+ // global added by QJMS_AddRequireGlobal
144
+ declare var require: RequireFunction;
145
+
146
+ // gets set per-module by QJMS_SetModuleImportMeta
147
+ interface ImportMeta {
148
+ /**
149
+ * A URL representing the current module.
150
+ *
151
+ * Usually starts with `file://`.
152
+ */
153
+ url: string;
154
+
155
+ /**
156
+ * Whether the current module is the "main" module, meaning that it is the
157
+ * entrypoint file that's been loaded, or, in other terms, the first
158
+ * user-authored module that's been loaded.
159
+ */
160
+ main: boolean;
161
+
162
+ /**
163
+ * Equivalent to `globalThis.require`. Provided for compatibility with tools
164
+ * that can leverage a CommonJS require function via `import.meta.require`.
165
+ */
166
+ require: RequireFunction;
167
+
168
+ /**
169
+ * Resolves a module specifier based on the current module's path.
170
+ *
171
+ * Equivalent to `globalThis.require.resolve`.
172
+ *
173
+ * Behaves similarly to [the browser import.meta.resolve](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/import.meta/resolve),
174
+ * but it does not ensure that the returned string is a valid URL, because it
175
+ * delegates directly to {@link Module.resolve} to resolve the name. If you
176
+ * want this to return URL strings, change `Module.resolve` and `Module.read`
177
+ * to work with URL strings.
178
+ */
179
+ resolve: RequireFunction["resolve"];
180
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@suchipi/quickjs",
3
3
  "description": "Suchipi's QuickJS Fork",
4
- "version": "0.4.0",
4
+ "version": "0.4.1",
5
5
  "main": "./npm/index.js",
6
6
  "bin": {
7
7
  "qjs": "npm/cli/qjs.js",