JavaScript snippet

The JavaScript snippet allows you to write, execute and inspect JavaScript code. It currently is supported on Mac and Linux. Windows is not yet supported.

The JavaScript snippet relies on the JSLink library to communicate with a NodeJS server via HTTP over TCP/IP.

Configuration

Presently, the execution depends on a NodeJS instance being installed locally. The location of node / NodeJS may be configured with:

"Set the location of the nodejs executable"
thisSnippet database properties jsLinkSettings 
	nodejsPath: '/usr/bin/node' asFileReference
  

At the same time, the JSLink runtime can be configured like this:

"Set the JavaScript runtime directory"
thisSnippet database properties jsLinkSettings 
	directory: '{dbParentDirectory}/js' asFileReference
  

After changing the configuration Lepiter needs to be restarted to pick up the new configuration.

Path segments in curly-braces, e.g. {imageDirectory} are FileLocator AbstractFileReference subclass: #FileLocator instanceVariableNames: 'origin' classVariableNames: 'Resolver' package: 'FileSystem-Core-Public' known origins. For a full list see:

FileLocator supportedOrigins sorted
  

Currently, the execution relies on a global instance of JSLinkApplication LanguageLinkApplication subclass: #JSLinkApplication instanceVariableNames: '' classVariableNames: '' package: 'JSLink-Global' .

Examples

40 + 2
  
var cwd = process.cwd();
cwd;
  

Variables defined in other code snippets, e.g. Pharo snippet and Python snippet, that can be JSON encoded will be shared between snippets within a page.

Installing JavaScript modules

If you have a script that you want to keep standalone and doesn't require detailed module management, individual modules may be installed using #installModule:, e.g.:

JSLinkApplication uniqueInstance installModule: 'MyModule'
  

Lepiter uses NodeJS to provide the JS environment, which in turn uses npm. If you want to manage the modules, e.g. update or specify versions, the environment's package.json file should be edited directly:

JSLinkApplication uniqueInstance settings workingDirectory / 'package.json'
  

Debugger Integration

If LeJSLinkSettings>>#serverDebugMode: serverDebugMode: aBooleanOrNil "Set a boolean indicating whether the server should be started in debug mode" ^ self updateProperty: #serverDebugMode value: aBooleanOrNil is configured true, a javascript debugger will be opened in Gt. See JavaScript debugger setup & example for details.

Tips

If the Node.JS server is restarted for any reason, all pages that have JS snippets that have been evaluated should be closed. This is required as the page bindings will have references to JS object registry IDs that no longer exist.