8.5 Executing JavaScript Code Snippets and Debugging MLE Modules

You can add, edit, save and execute JavaScript code snippets using the Snippets tab in the right pane of the MLE JS page. Code snippets saved in the browser and file system are listed under the Snippet tab in the left pane.

JavaScript snippets can also be used to test and debug MLE modules using a debug specification. For more information about debug specifications, see Post-Execution Debugging in the JavaScript Developer's Guide.

Note:

SQL worksheet supports the execution of JavaScript code in Oracle Database release 21c. These saved JavaScript worksheets will also appear under the Snippets tab in the left pane.

Executing a JavaScript Code Snippet

To execute a code snippet:

  1. In the right pane, select the Snippet tab and enter the JavaScript snippet.

    You can do this in one of two ways:

    • Type and enter the code, or copy and paste the code.
    • Click Open File in the toolbar to browse and open the file that contains the code.
  2. Click the down arrow next to Snippet in the toolbar and select Save As. Enter a name for the code snippet and save.
  3. If required (such as when testing MLE modules), select the preferred environment.
  4. Click Run Snippet. The Output tab in the lower pane displays the results of the code execution.

Debugging a JavaScript MLE Module

To debug an MLE module:

  1. Enter the code snippet, save it and select the environment. See steps 1-3 in Executing a JavaScript Code Snippet.
  2. In the Snippet toolbar, from the Debug Specification menu, click New Debug Specification.

    The Create Debug Specification panel appears.

  3. In the Create Debug Specification panel, do the following:
    1. In the right side of the panel, select the module to debug.
    2. In the left side of the panel, select the JSON template you want: Snapshot or Watch. For more information about these templates, see Debugpoint Actions in theJavaScript Developer's Guide.
    3. In the JSON template, the name of the MLE module to debug is specified using the name field and the location within the module where the debug information is to be collected is specified using the line field. To specify the condition that will trigger the debug action, use the condition field. See Example: Debugging a JavaScript Module.
    4. Click Create,
  4. In the MLS JS page, select the Snippet tab and in the Debug Specification field, select the created debug specification.
  5. From the Snippet toolbar, click Debug Snippet.

    The debug information appears under the Debug Console tab in the lower pane.

See Example: Debugging a JavaScript Module for an example of how to debug a JavaScript MLE module.

8.5.1 Example: Debugging a JavaScript Module

This example demonstrates how to debug a JavaScript module.

Create the JavaScript Module

  1. In the MLE JS page, in the right pane, select the Editor tab and enter the following JavaScript function:
    export function fib( n ) {
        if ( n < 2 ) return 1;
        let n1 = { value : fib( n - 1 ), id : 'n1' };
        let n2 = { value : fib( n - 2 ), id : 'n2' };
        return n1.value + n2.value;
    }
  2. In the Name field, enter fib_mod and click Save.

    The created module appears in the left Navigator pane under Modules.

Create the MLE Environment

  1. In the Navigator tab, from the object type selector, select Environments. Click Object submenu.
  2. In the Create MLE Environment panel, do the following:
    1. In the Name field, enter fib_env.
    2. From the Available Modules field, select the module FIB_MOD and click Add Selected Modules to move the module to the Imported Modules field.
    3. Click Create.

    The new environment appears in the left pane under Environments.

Enter the Code Snippet

  1. In the right pane, select the Snippet tab and enter the code snippet.

    Select the environment fib_mod to set the execution context. If the environment does not display, click Reload Environments and then expand the dropdown again.

    Enter the following code:

    (async ()=>{
    const aa = await import('FIB_MOD') ;
    console.log(aa.fib(6));
    })()

    Click the dropdown in the toolbar, select Save As and enter a name for the snippet.

  2. To execute the snippet, click Run Snippet and the output is displayed in the lower right pane under the Output tab.

Create the Debug Specification

  1. To create a new debug specification, in the toolbar, click the Debug Specification Menu and select New Debug Specification.
  2. In the Create Debug Specification panel, do the following:
    1. Select FIB_MOD from the Module drop-down list.
    2. In the left side of the panel, select Snapshot Template.
    3. In the JSON document, edit the following:

      • name: Enter the module name.
      • line: Enter 5 to identify the position in the module code.
      • condition: Enter n > 4.
    4. Enter the name fib_debug for the debug specification.
    5. Click Create.
  3. From the Snippet toolbar, click Debug Snippet.

    The output appears under the Debug Console tab in the lower pane.

    If you position the cursor over the debug points, the corresponding position in the module code is displayed.