Home News DOC Class DL Others Rice
DOC  >  Tutorial - Basics  >  Direct execution
Direct execution of JavaScript

Purpose of this page

In the previous page, we explained to execute JavaScript code via the Rice. In this page, we'll learn how to execute JavaScript directly.


Table of contents:


Drop file

File selection dialog

Re-execution

Immediately Invoked Function Expression (IIFE)

File I/O

Drop file

You can execute JavaScript by dropping a .js file on the script tile.

Script tile is colored area the right side of the browser. If you drop it on the left tile, it will be executed in the Main-browser, and if it is on the right, it will be executed in the Sub-browser.

File selection dialog

You can also execute JavaScript by selecting a .js file from the file selection dialog. The file selection dialog will open by pressing the Alt + R keys or clicking the script tile.

Re-execution

Alt + E will re-execute the previously executed script.

Immediately Invoked Function Expression (IIFE)

Direct execution does not convert JavaScript code to IIFE by default, but as mentioned on the previous page, conversion to IIFE has many advantages.

Therefore, we prepared conversion method to IIFE. In direct execution, the conversion is performed by writing an instruction directly in the JavaScript code.

"///immed" and "///async" are instructions of conversion. If the beginning of the JavaScript code is these strings, conversion will be performed.

1:

alert(token);

Add "///immed" to the above JavaScript code.

1:

///immed

2:

alert(token);

Conversion is as follows. The 1st and 4th lines are added.

1:

(function(token){

2:

///immed

3:

alert(token);

4:

}("0f8fbd59d9cb769fa16570867728950e"));

Add "///async" to the above JavaScript code.

1:

///async

2:

alert(token);

Conversion is as follows. The 1st and 4th lines are added.

1:

(async function(token){

2:

///async

3:

alert(token);

4:

}("0f8fbd59d9cb769fa16570867728950e"));

File I/O

Direct execution cannot receive the return value from JavaScript. Instead, a simple file I/O is provided as extensions.


JavaScript : window.cooker.storage object

1:Write(token, filename, data, append)Write the text data to a file. It is asynchronous function.
2:Read(token, filename)Read text data from a file. It is asynchronous function.

Write to a file:

1:

window.cooker.storage.Write(token, "example.txt", "data string", true);

First argumenttokenA string that represents 32-digit number. It is set at the time of the conversion.
SecondfilenameA file name.
ThirddataA string for data.
FourthappendA boolean value. Append is true. Overwrite is false.

Give only a file name as the third argument. File I/O is restricted to direct child files in the storage subdirectory within Cooker's execution directory.

If a path is given, only the file name is extracted from the path and treated as a direct child file of the storage subdirectory.


Read from a file:

1:

let data = await window.cooker.storage.Read(token, "example.txt");

First argumenttokenA string that represents 32-digit number. It is set at the time of the conversion.
SecondfilenameA file name.

Note to the file name. It is restricted similar to Write().

Note that the entire data in the file is retrieved.

You can use the other extensions described on the previous page as well.

Previous
Copyright © Cooker All rights reserved.