Part I: Implement ES2015 module system. (#2599)

JerryScript-DCO-1.0-Signed-off-by: Daniel Vince vinced@inf.u-szeged.hu
This commit is contained in:
Daniel Vince
2019-03-18 16:22:06 +01:00
committed by László Langó
parent 3d3e6fdf58
commit 4123f35a3b
45 changed files with 2095 additions and 8 deletions
+26
View File
@@ -83,6 +83,32 @@ information.
void jerry_port_print_char (char c);
```
### ES2015 Module system helper functions
The import statement requires two specific functions for opening and closing files (the modules) port specific.
```c
/**
* Opens file with the given path and reads its source.
* @return the source of the file
*/
uint8_t *
jerry_port_read_source (const char *file_name_p, /**< file name */
size_t *out_size_p) /**< [out] read bytes */
{
// open file from given path
// return its source
} /* jerry_port_read_source */
/**
* Release the previously opened file's content.
*/
void
jerry_port_release_source (uint8_t *buffer_p) /**< buffer to free */
{
free (buffer_p);
} /* jerry_port_release_source */
```
## Date
+132
View File
@@ -0,0 +1,132 @@
# ES6 module support for JerryScript
The module system allows users to write import and export statements in scripts. Therefore the logic of the application could be separated in custom modules.
The standard's relevant part can be found [here](https://www.ecma-international.org/ecma-262/6.0/#sec-modules).
## General
If the main script contains import statements, then Jerry opens and runs the appropriate scripts before the main script (as the standard says). The script's and the module's extension is `.js`, custom extensions are unnecessary.
main.js
```js
import { secret_number } from "./module.js"
print (secret_number);
```
module.js
```js
var secret_number = 42;
export secret_number;
```
## Supported features
* import variable or function
* add alias name to the imported variable (function)
* export variable or function
* add alias name to the exported variable (function)
### Example
```js
import {
engine,
version as v
} from "./module.js"
import { getFeatureDetails } from "./module_2.js"
var version = "v3.1415";
print("> main.js");
print(">> Engine: " + engine);
print(">> Version: " + v);
print (">> " + getFeatureDetails());
print (">> Script version: " + version);
```
```js
// module.js
var _engine = "JerryScript";
export _engine as engine;
export var version = "1.0 (e92ae0fb)";
```
```js
// module_2.js
var featureName = "EcmaScript 2015 modules";
var year = 2018;
export function getFeatureDetails() {
return "Feature name: " + featureName + " | developed in " + year;
}
```
## Unsupported features
* **snapshot**
* errors from the imported scripts
* redirection ( `export { a, b } from 'module.js'` )
* default import and export
* `import b from 'module.js'`
* `export default b`,
* whole module import statements
* `import * from 'module.js`
* `import { * as module } from 'module.js`
* object freezing ( `Object.freeze (this)` )
### Redirection
An export statement can import variables from a custom module and export it directly from the current script. This statement is called redirection. In this case the `export { b } from 'module2.js'` works as the `b` was imported before then exported as a local variable.
```js
import { a, b } from 'module.js'
print (a + b);
```
```js
// module.js
export var a = 2;
export { b } from 'module2.js'
```
```js
// module2.js
export var b = 40;
```
### Default imports and exports
TODO: This part is going to be written in the next part of the patch.
### Import the whole module
The whole module can be imported. In this case the `m` object would contain the exported parts of the module. If the import is not aliased, the `global object` would contain the exports.
```js
import { * as m } from "./module.js"
print (m.secret_number);
print (m.getPrettifiedNumber());
print (m.api.version);
```
```js
// module.js
var secret_number = 42;
export secret_number;
export function getPrettifiedNumber() {
return "*** " + secret_number + " ***";
}
export { ble as api } from "./ble.js";
```