Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ dist: trusty

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

# All the job definitions in the matrix.
matrix:
Expand Down
26 changes: 26 additions & 0 deletions docs/05.PORT-API.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
132 changes: 132 additions & 0 deletions docs/14.MODULE-SYSTEM.md
Original file line number Diff line number Diff line change
@@ -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";
```
1 change: 1 addition & 0 deletions jerry-core/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
# define CONFIG_DISABLE_ES2015_SYMBOL_BUILTIN
# define CONFIG_DISABLE_ES2015_TEMPLATE_STRINGS
# define CONFIG_DISABLE_ES2015_TYPEDARRAY_BUILTIN
# define CONFIG_DISABLE_ES2015_MODULE_SYSTEM
#endif /* CONFIG_DISABLE_ES2015 */

/**
Expand Down
4 changes: 4 additions & 0 deletions jerry-core/ecma/base/ecma-init-finalize.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ ecma_finalize (void)
{
jmem_unregister_free_unused_memory_callback (ecma_free_unused_memory);

#ifndef CONFIG_DISABLE_ES2015_MODULE_SYSTEM
ecma_module_finalize_lex_envs ();
#endif /* !CONFIG_DISABLE_ES2015_MODULE_SYSTEM */

ecma_finalize_global_lex_env ();
ecma_finalize_builtins ();
ecma_gc_run (JMEM_FREE_UNUSED_MEMORY_SEVERITY_LOW);
Expand Down
Loading