Skip to content

Commit 4123f35

Browse files
vincedaniLaszloLango
authored andcommitted
Part I: Implement ES2015 module system. (#2599)
JerryScript-DCO-1.0-Signed-off-by: Daniel Vince vinced@inf.u-szeged.hu
1 parent 3d3e6fd commit 4123f35

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+2095
-8
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ dist: trusty
66

77
# Default job task: run tests as defined in the $OPT environment variable.
88
# Jobs can redefine the 'script' stage in the matrix below.
9-
script: tools/run-tests.py $OPTS
9+
script: travis_wait 20 tools/run-tests.py $OPTS
1010

1111
# All the job definitions in the matrix.
1212
matrix:

docs/05.PORT-API.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,32 @@ information.
8383
void jerry_port_print_char (char c);
8484
```
8585

86+
### ES2015 Module system helper functions
87+
88+
The import statement requires two specific functions for opening and closing files (the modules) port specific.
89+
90+
```c
91+
/**
92+
* Opens file with the given path and reads its source.
93+
* @return the source of the file
94+
*/
95+
uint8_t *
96+
jerry_port_read_source (const char *file_name_p, /**< file name */
97+
size_t *out_size_p) /**< [out] read bytes */
98+
{
99+
// open file from given path
100+
// return its source
101+
} /* jerry_port_read_source */
102+
103+
/**
104+
* Release the previously opened file's content.
105+
*/
106+
void
107+
jerry_port_release_source (uint8_t *buffer_p) /**< buffer to free */
108+
{
109+
free (buffer_p);
110+
} /* jerry_port_release_source */
111+
```
86112
87113
## Date
88114

docs/14.MODULE-SYSTEM.md

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
# ES6 module support for JerryScript
2+
3+
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.
4+
The standard's relevant part can be found [here](https://www.ecma-international.org/ecma-262/6.0/#sec-modules).
5+
6+
## General
7+
8+
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.
9+
10+
main.js
11+
12+
```js
13+
import { secret_number } from "./module.js"
14+
15+
print (secret_number);
16+
```
17+
18+
module.js
19+
20+
```js
21+
var secret_number = 42;
22+
23+
export secret_number;
24+
```
25+
26+
## Supported features
27+
28+
* import variable or function
29+
* add alias name to the imported variable (function)
30+
* export variable or function
31+
* add alias name to the exported variable (function)
32+
33+
### Example
34+
35+
```js
36+
import {
37+
engine,
38+
version as v
39+
} from "./module.js"
40+
41+
import { getFeatureDetails } from "./module_2.js"
42+
43+
var version = "v3.1415";
44+
45+
print("> main.js");
46+
47+
print(">> Engine: " + engine);
48+
print(">> Version: " + v);
49+
50+
print (">> " + getFeatureDetails());
51+
print (">> Script version: " + version);
52+
```
53+
54+
```js
55+
// module.js
56+
var _engine = "JerryScript";
57+
export _engine as engine;
58+
59+
export var version = "1.0 (e92ae0fb)";
60+
```
61+
62+
```js
63+
// module_2.js
64+
var featureName = "EcmaScript 2015 modules";
65+
var year = 2018;
66+
67+
export function getFeatureDetails() {
68+
return "Feature name: " + featureName + " | developed in " + year;
69+
}
70+
```
71+
72+
## Unsupported features
73+
74+
* **snapshot**
75+
* errors from the imported scripts
76+
* redirection ( `export { a, b } from 'module.js'` )
77+
* default import and export
78+
* `import b from 'module.js'`
79+
* `export default b`,
80+
* whole module import statements
81+
* `import * from 'module.js`
82+
* `import { * as module } from 'module.js`
83+
* object freezing ( `Object.freeze (this)` )
84+
85+
### Redirection
86+
87+
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.
88+
89+
```js
90+
import { a, b } from 'module.js'
91+
92+
print (a + b);
93+
```
94+
95+
```js
96+
// module.js
97+
export var a = 2;
98+
export { b } from 'module2.js'
99+
```
100+
101+
```js
102+
// module2.js
103+
export var b = 40;
104+
```
105+
106+
### Default imports and exports
107+
108+
TODO: This part is going to be written in the next part of the patch.
109+
110+
### Import the whole module
111+
112+
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.
113+
114+
```js
115+
import { * as m } from "./module.js"
116+
117+
print (m.secret_number);
118+
print (m.getPrettifiedNumber());
119+
print (m.api.version);
120+
```
121+
122+
```js
123+
// module.js
124+
var secret_number = 42;
125+
export secret_number;
126+
127+
export function getPrettifiedNumber() {
128+
return "*** " + secret_number + " ***";
129+
}
130+
131+
export { ble as api } from "./ble.js";
132+
```

jerry-core/config.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
# define CONFIG_DISABLE_ES2015_SYMBOL_BUILTIN
4949
# define CONFIG_DISABLE_ES2015_TEMPLATE_STRINGS
5050
# define CONFIG_DISABLE_ES2015_TYPEDARRAY_BUILTIN
51+
# define CONFIG_DISABLE_ES2015_MODULE_SYSTEM
5152
#endif /* CONFIG_DISABLE_ES2015 */
5253

5354
/**

jerry-core/ecma/base/ecma-init-finalize.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ ecma_finalize (void)
6161
{
6262
jmem_unregister_free_unused_memory_callback (ecma_free_unused_memory);
6363

64+
#ifndef CONFIG_DISABLE_ES2015_MODULE_SYSTEM
65+
ecma_module_finalize_lex_envs ();
66+
#endif /* !CONFIG_DISABLE_ES2015_MODULE_SYSTEM */
67+
6468
ecma_finalize_global_lex_env ();
6569
ecma_finalize_builtins ();
6670
ecma_gc_run (JMEM_FREE_UNUSED_MEMORY_SEVERITY_LOW);

0 commit comments

Comments
 (0)