Skip to content

Commit

Permalink
JS MOO-RPC fixes / additions:
Browse files Browse the repository at this point in the history
  * Fix parsing of results
  * Add 'get_property'
  • Loading branch information
rdaum committed Nov 5, 2023
1 parent a4f2dd5 commit 7e59f14
Showing 1 changed file with 22 additions and 9 deletions.
31 changes: 22 additions & 9 deletions crates/web-host/src/client/root.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
} else if (typeof json === "object") {
if (json["error_code"]) {
return json["error_name"];
} else if (json["oid"]) {
} else if (json["oid"] != null) {
return "#" + json["oid"];
} else if (Array.isArray(json)) {
let result = "{";
Expand All @@ -42,13 +42,24 @@
}
}

function transform_args(args) {
let result = [];
for (let i = 0; i < args.length; i++) {
result.push(json_to_moo(args[i]));
}
return result.join(", ");
}

// Recursively descend a JSON result from eval, and turns object references into MooRPCObjects.
function transform_eval(json) {
// Empty json is null, so return null.
if (json == null) {
return null;
}
if (typeof json != "object") {
return json;
}
if (json["oid"]) {
if (json["oid"] != null) {
return new MoorRPCObject(json["oid"], context.auth_token);
} else if (Array.isArray(json)) {
let result = [];
Expand Down Expand Up @@ -76,18 +87,20 @@
// "return #<object_id>:<verb>(<args>)"
async invoke_verb(verb_name, args) {
let self = json_to_moo(this.object_id)
let verbargs = json_to_moo(args);
let expr = "return " + self + ":" + verb_name + "(" + verbargs + ")";
let args_str = transform_args(args);
let expr = "return #" + self + ":" + verb_name + "(" + args_str + ");";
return perform_eval(this.auth_token, expr);
}

async get_property(property_name) {
let self = json_to_moo(this.object_id);
let expr = "return #" + self + "." + property_name + ";";
return perform_eval(this.auth_token, expr);
}
}

async function call_builtin(auth_token, builtin, args) {
let argument_elements = [];
for (let i = 0; i < args.length; i++) {
argument_elements.push(json_to_moo(args[i]));
}
let args_str = argument_elements.join(", ");
let args_str = transform_args(args);
let expr = "return " + builtin + "(" + args_str + ");";
return perform_eval(auth_token, expr);
}
Expand Down

0 comments on commit 7e59f14

Please sign in to comment.