-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8ba3be0
commit 1806f4a
Showing
6 changed files
with
139 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
Submodule duktape
updated
5 files
+2 −0 | config/sandbox_config.yaml | |
+4 −4 | src-new/duk_config.h | |
+139 −139 | src-new/duk_source_meta.json | |
+452 −513 | src-new/duktape.c | |
+3 −3 | src-new/duktape.h |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!' |