mirror of
https://github.com/Drop-OSS/droplet.git
synced 2025-11-09 20:12:18 +10:00
feat: start of scripting engine
This commit is contained in:
923
Cargo.lock
generated
923
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
@ -16,7 +16,6 @@ napi = { version = "3.0.0-beta.11", default-features = false, features = [
|
||||
] }
|
||||
napi-derive = "3.0.0-beta.11"
|
||||
hex = "0.4.3"
|
||||
serde_json = "1.0.128"
|
||||
md5 = "0.7.0"
|
||||
time-macros = "0.2.22"
|
||||
time = "0.3.41"
|
||||
@ -27,6 +26,10 @@ tokio-util = { version = "0.7.15", features = ["codec"] }
|
||||
rawzip = "0.3.0"
|
||||
dyn-clone = "1.0.20"
|
||||
flate2 = "1.1.2"
|
||||
rhai = "1.22.2"
|
||||
mlua = { version = "0.11.2", features = ["luajit"] }
|
||||
boa_engine = "0.20.0"
|
||||
serde_json = "1.0.143"
|
||||
|
||||
[package.metadata.patch]
|
||||
crates = ["rawzip"]
|
||||
|
||||
62
__test__/script.spec.mjs
Normal file
62
__test__/script.spec.mjs
Normal file
@ -0,0 +1,62 @@
|
||||
import test from "ava";
|
||||
import { ScriptEngine } from "../index.js";
|
||||
|
||||
test("lua syntax fail", (t) => {
|
||||
const scriptEngine = new ScriptEngine();
|
||||
|
||||
const luaIshCode = `
|
||||
print("hello world);
|
||||
`;
|
||||
|
||||
try {
|
||||
const script = scriptEngine.buildLuaScript(luaIshCode);
|
||||
} catch {
|
||||
return t.pass();
|
||||
}
|
||||
t.fail();
|
||||
});
|
||||
|
||||
test("js syntax fail", (t) => {
|
||||
const scriptEngine = new ScriptEngine();
|
||||
|
||||
const jsIshCode = `
|
||||
const v = "hello world;
|
||||
`;
|
||||
|
||||
try {
|
||||
const script = scriptEngine.buildJsScript(jsIshCode);
|
||||
} catch {
|
||||
return t.pass();
|
||||
}
|
||||
t.fail();
|
||||
});
|
||||
|
||||
test("js", (t) => {
|
||||
const scriptEngine = new ScriptEngine();
|
||||
|
||||
const jsModule = `
|
||||
const v = "1" + "2";
|
||||
["1", "2", "3", v]
|
||||
`;
|
||||
|
||||
const script = scriptEngine.buildJsScript(jsModule);
|
||||
|
||||
scriptEngine.fetchStrings(script);
|
||||
|
||||
t.pass();
|
||||
});
|
||||
|
||||
test("lua", (t) => {
|
||||
const scriptEngine = new ScriptEngine();
|
||||
|
||||
const luaModule = `
|
||||
local arr = {"1", "2"};
|
||||
return arr;
|
||||
`;
|
||||
|
||||
const script = scriptEngine.buildLuaScript(luaModule);
|
||||
|
||||
scriptEngine.fetchStrings(script);
|
||||
|
||||
t.pass();
|
||||
});
|
||||
13
index.d.ts
vendored
13
index.d.ts
vendored
@ -15,6 +15,19 @@ export declare class JsDropStreamable {
|
||||
getStream(): any
|
||||
}
|
||||
|
||||
export declare class Script {
|
||||
|
||||
}
|
||||
|
||||
export declare class ScriptEngine {
|
||||
constructor()
|
||||
buildRahiScript(content: string): Script
|
||||
buildLuaScript(content: string): Script
|
||||
buildJsScript(content: string): Script
|
||||
execute(script: Script): void
|
||||
fetchStrings(script: Script): Array<string>
|
||||
}
|
||||
|
||||
export declare function callAltThreadFunc(tsfn: ((err: Error | null, ) => any)): void
|
||||
|
||||
export declare function generateClientCertificate(clientId: string, clientName: string, rootCa: string, rootCaPrivate: string): Array<string>
|
||||
|
||||
2
index.js
2
index.js
@ -378,6 +378,8 @@ if (!nativeBinding) {
|
||||
module.exports = nativeBinding
|
||||
module.exports.DropletHandler = nativeBinding.DropletHandler
|
||||
module.exports.JsDropStreamable = nativeBinding.JsDropStreamable
|
||||
module.exports.Script = nativeBinding.Script
|
||||
module.exports.ScriptEngine = nativeBinding.ScriptEngine
|
||||
module.exports.callAltThreadFunc = nativeBinding.callAltThreadFunc
|
||||
module.exports.generateClientCertificate = nativeBinding.generateClientCertificate
|
||||
module.exports.generateManifest = nativeBinding.generateManifest
|
||||
|
||||
@ -4,6 +4,7 @@
|
||||
pub mod manifest;
|
||||
pub mod ssl;
|
||||
pub mod version;
|
||||
pub mod script;
|
||||
|
||||
#[macro_use]
|
||||
extern crate napi_derive;
|
||||
|
||||
128
src/script/mod.rs
Normal file
128
src/script/mod.rs
Normal file
@ -0,0 +1,128 @@
|
||||
use boa_engine::{Context, JsValue, Source};
|
||||
use mlua::{FromLuaMulti, Function, Lua};
|
||||
use napi::Result;
|
||||
use rhai::AST;
|
||||
|
||||
pub enum ScriptType {
|
||||
Rhai,
|
||||
Lua,
|
||||
Javascript,
|
||||
}
|
||||
|
||||
#[napi]
|
||||
pub struct Script(ScriptInner);
|
||||
|
||||
pub enum ScriptInner {
|
||||
Rhai { script: AST },
|
||||
Lua { script: Function },
|
||||
Javascript { script: boa_engine::Script },
|
||||
}
|
||||
|
||||
#[napi]
|
||||
pub struct ScriptEngine {
|
||||
rhai_engine: rhai::Engine,
|
||||
lua_engine: Lua,
|
||||
js_engine: Context,
|
||||
}
|
||||
|
||||
#[napi]
|
||||
impl ScriptEngine {
|
||||
#[napi(constructor)]
|
||||
pub fn new() -> Self {
|
||||
ScriptEngine {
|
||||
rhai_engine: rhai::Engine::new(),
|
||||
lua_engine: Lua::new(),
|
||||
js_engine: Context::default(),
|
||||
}
|
||||
}
|
||||
|
||||
#[napi]
|
||||
pub fn build_rahi_script(&self, content: String) -> Result<Script> {
|
||||
let script = self
|
||||
.rhai_engine
|
||||
.compile(content.clone())
|
||||
.map_err(|e| napi::Error::from_reason(e.to_string()))?;
|
||||
Ok(Script(ScriptInner::Rhai { script }))
|
||||
}
|
||||
|
||||
#[napi]
|
||||
pub fn build_lua_script(&self, content: String) -> Result<Script> {
|
||||
let func = self
|
||||
.lua_engine
|
||||
.load(content.clone())
|
||||
.into_function()
|
||||
.map_err(|e| napi::Error::from_reason(e.to_string()))?;
|
||||
Ok(Script(ScriptInner::Lua { script: func }))
|
||||
}
|
||||
|
||||
#[napi]
|
||||
pub fn build_js_script(&mut self, content: String) -> Result<Script> {
|
||||
let source = Source::from_bytes(content.as_bytes());
|
||||
let script = boa_engine::Script::parse(source, None, &mut self.js_engine)
|
||||
.map_err(|e| napi::Error::from_reason(e.to_string()))?;
|
||||
|
||||
Ok(Script(ScriptInner::Javascript { script }))
|
||||
}
|
||||
|
||||
fn execute_rhai_script<T>(&self, ast: &AST) -> Result<T>
|
||||
where
|
||||
T: Clone + 'static,
|
||||
{
|
||||
let v = self
|
||||
.rhai_engine
|
||||
.eval_ast::<T>(ast)
|
||||
.map_err(|e| napi::Error::from_reason(e.to_string()))?;
|
||||
|
||||
Ok(v)
|
||||
}
|
||||
|
||||
fn execute_lua_script<T>(&self, function: &Function) -> Result<T>
|
||||
where
|
||||
T: FromLuaMulti,
|
||||
{
|
||||
let v = function
|
||||
.call::<T>(())
|
||||
.map_err(|e| napi::Error::from_reason(e.to_string()))?;
|
||||
Ok(v)
|
||||
}
|
||||
|
||||
fn execute_js_script(&mut self, func: &boa_engine::Script) -> Result<JsValue> {
|
||||
let v = func
|
||||
.evaluate(&mut self.js_engine)
|
||||
.map_err(|e| napi::Error::from_reason(e.to_string()))?;
|
||||
|
||||
Ok(v)
|
||||
}
|
||||
|
||||
#[napi]
|
||||
pub fn execute(&mut self, script: &mut Script) -> Result<()> {
|
||||
match &script.0 {
|
||||
ScriptInner::Rhai { script } => {
|
||||
self.execute_rhai_script::<()>(script)?;
|
||||
}
|
||||
ScriptInner::Lua { script } => {
|
||||
self.execute_lua_script::<()>(script)?;
|
||||
}
|
||||
ScriptInner::Javascript { script } => {
|
||||
self.execute_js_script(script)?;
|
||||
}
|
||||
};
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[napi]
|
||||
pub fn fetch_strings(&mut self, script: &mut Script) -> Result<Vec<String>> {
|
||||
Ok(match &script.0 {
|
||||
ScriptInner::Rhai { script } => self.execute_rhai_script(script)?,
|
||||
ScriptInner::Lua { script } => self.execute_lua_script(script)?,
|
||||
ScriptInner::Javascript { script } => {
|
||||
let v = self.execute_js_script(script)?;
|
||||
|
||||
serde_json::from_value(
|
||||
v.to_json(&mut self.js_engine)
|
||||
.map_err(|e| napi::Error::from_reason(e.to_string()))?,
|
||||
).map_err(|e| napi::Error::from_reason(e.to_string()))?
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user