///|
#export_name("value_to_json")
pub fn value_to_json(value : RuntimeValue) -> String {
value.to_json().stringify()
}
///|
#export_name("value_to_string")
pub fn value_to_string(value : RuntimeValue) -> String {
value.to_string()
}
///|
pub(open) trait ToRuntime {
fn to_runtime(Self) -> RuntimeValue
}
///|
pub impl ToRuntime for RuntimeValue with fn to_runtime(self) {
self
}
///|
pub impl ToRuntime for Int32 with fn to_runtime(value) {
Int(value, raw=None)
}
///|
pub impl ToRuntime for Bool with fn to_runtime(value) {
Bool(value)
}
///|
pub impl ToRuntime for String with fn to_runtime(value) {
String(value)
}
///|
pub impl ToRuntime for Double with fn to_runtime(value) {
Double(value)
}
///|
pub fn MoonBitVM::create(
log~ : Bool = false,
modules~ : Array[RuntimeModule] = [],
) -> MoonBitVM {
MoonBitVM(log~, modules~)
}
///|
#export_name("eval_result_to_string")
pub fn eval_result_to_string(result : EvalResult) -> String {
match result {
Success(value, _) => value.to_string()
Error(msg, _) => "Error: " + msg
}
}
///|
#export_name("test_result_to_string")
pub fn test_result_to_string(result : TestResult) -> String {
result.to_string()
}
///|
#export_name("code_to_ast")
pub fn code_to_ast(code : String) -> String {
match @core.parse_eval_code(code) {
Ok(EvalExpr(expr)) => @json.to_json(expr).stringify()
Ok(EvalTop(impls, ..)) => @json.to_json(impls).stringify()
Err(msg) => msg
}
}
///|
#export_name("add_extern_fn")
pub fn add_extern_fn(
vm : MoonBitVM,
name : String,
func : @core.RuntimeFunction,
) -> Unit {
vm.interpreter.add_extern_fn(name, func)
}
///|
#export_name("add_embedded_fn")
pub fn add_embedded_fn(
vm : MoonBitVM,
name : String,
func : @core.RuntimeFunction,
) -> Unit {
vm.interpreter.add_embedded_fn(name, func)
}
///|
#export_name("add_embedded_method")
pub fn add_embedded_method(
vm : MoonBitVM,
name : String,
method_name : String,
func : @core.RuntimeFunction,
) -> Unit {
vm.interpreter.add_embedded_method(name, method_name, func)
}