Skip to content

Commit

Permalink
Update duktape module, add docs
Browse files Browse the repository at this point in the history
  • Loading branch information
angrymouse committed Nov 8, 2023
1 parent 8ba3be0 commit 1806f4a
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 15 deletions.
7 changes: 7 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Copyright 2023 Mykyta Rykov

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
104 changes: 104 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
# Glomium

Glomium is a powerful JavaScript module that serves as the core for [Glome](https://github.com/rareweave/glome), a smart contract engine optimized for creating performant, scalable, and secure smart contracts on Arweave.
By providing JavaScript bindings to the Duktape JS engine, Glomium enables the embedding and secure execution of JavaScript code within a Node.js environment (eval() but isolated), with custom resource constraints such as gas limits and memory costs.

## Installation

To install Glomium to your Node.js project, just pull it off npm:

```bash
npm install glomium
```

Cloud-built binaries will be automatically pulled, no need to have node-gyp installed.

## Usage

Below is a simple example on how to use Glomium in your projects:

```js

const Glomium = require('glomium');

// Initialize a new Glomium instance with optional configuration
const glomium = new Glomium({
gas: {
limit: 100000, // Set the maximum amount of gas allowed for the execution context
memoryByteCost: 1 // Define the gas cost per byte of memory used
}
});

// Set a global variable within the Duktape context
glomium.set('hello', 'world');

// Retrieve the value of a global variable from the Duktape context
const world = glomium.get('hello');

// Run JavaScript code in the Duktape context
const result = glomium.run(`
function greet(name) {
return 'Hello, ' + name + '!';
}
greet(hello);
`);

console.log(result); // Should output 'Hello, world!'
```

## API

### `new Glomium(config)`

Creates a new instance of Glomium with optional configuration.

- **Parameters**
- `config` _(Object)_: The configuration object for the Glomium instance.
- `gas` _(Object)_: Contains the gas configuration for the execution context.
- `limit` _(number)_: The maximum amount of gas the execution context is allowed to use (default: `100000`).
- `memoryByteCost` _(number)_: The cost of gas per byte of memory used by the context (default: `1`).

### `glomium.set(name, value)`

Sets a global variable within the Duktape execution context.

- **Parameters**
- `name` _(string)_: The name of the global variable to set.
- `value` _(any)_: The value to set for the global variable.

### `glomium.get(name)`

Retrieves the value of a global variable from the Duktape execution context.

- **Parameters**
- `name` _(string)_: The name of the global variable to get.

### `glomium.run(code)`

Executes a string of JavaScript code within the Duktape execution context and returns the result.

- **Parameters**
- `code` _(string)_: The JavaScript code to execute.

## Building
You can build Glomium from source by executing following commands:

```bash
git clone https://github.com/rareweave/glomium
cd glomium
git submodule update --init
cd duktape
python2 tools/configure.py --output-directory src-new --source-directory src-input --config-metadata config --option-file config/sandbox_config.yaml
cd ..
node-gyp rebuild
```

This will produce required `build` directory.

## Contributions

Contributions to Glomium are welcomed as it is a foundational component of the Glome smart contract engine. Feel free to fork, submit pull requests, or raise issues.

## License

Glomium is distributed under the MIT license.
2 changes: 1 addition & 1 deletion duktape
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Glomium {
return this;
}
get(name) {
return duktapeBindings.getGlobal(this.context,name, value);
return duktapeBindings.getGlobal(this.context,name);
}
run(code) {
return duktapeBindings.evalString(this.context,code);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "glomium",
"version": "1.0.7",
"version": "1.0.8",
"description": "Duktape bindings for node.js",
"main": "index.js",
"scripts": {
Expand Down
37 changes: 25 additions & 12 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,25 @@
const Glomium = require("./")

console.log(
new Glomium({
gas: {
limit: 1000000,
memoryByteCost: 1
}
})
.set("test", 5)
.run(`(test+2)`)
)
const Glomium = require('./');

// Create a new Glomium instance with custom configuration
const glomium = new Glomium({
gas: {
limit: 1000000, // The maximum amount of gas the execution environment can use
memoryByteCost: 1 // The gas cost per byte of memory used
}
});

// Set global variables in the Duktape context
glomium.set('hello', 'world');

// Get global variables from the Duktape context
const world = glomium.get('hello');

// Run some JavaScript code
const result = glomium.run(`
function greet(name) {
return 'Hello, ' + name + '!';
}
greet(hello);
`);

console.log(result); // Should output 'Hello, world!'

0 comments on commit 1806f4a

Please sign in to comment.