Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions js/src/builtins/filterkeys.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { truthy } from "../runtimeValues";
import { pushRuntimeValueToStack } from "../stackManip";
import { BuiltinFunction } from "../types";
import { arity } from "../util";

import { arity, validateType } from "../util";

const filterkeys: BuiltinFunction = arity(2, (args, stack, exec) => {
const evaluated = exec(args[1], stack);
const evaluated = validateType("object", exec(args[1], stack));
const results = {};
for (let i in evaluated) {
if (
Expand All @@ -18,4 +17,4 @@ const filterkeys: BuiltinFunction = arity(2, (args, stack, exec) => {
return results;
});

export default filterkeys;
export default filterkeys;
5 changes: 2 additions & 3 deletions js/src/builtins/filtervalues.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { truthy } from "../runtimeValues";
import { pushRuntimeValueToStack } from "../stackManip";
import { BuiltinFunction } from "../types";
import { arity } from "../util";

import { arity, validateType } from "../util";

const filtervalues: BuiltinFunction = arity(2, (args, stack, exec) => {
const evaluated = exec(args[1], stack);
const evaluated = validateType("object", exec(args[1], stack));
const results = {};
for (let i in evaluated) {
if (
Expand Down
9 changes: 8 additions & 1 deletion js/src/builtins/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,13 @@ const divide = numericBinaryOperator((a, b) => {
return a / b;
});

const modulo = numericBinaryOperator((a, b) => {
if (b === 0) {
throw new RuntimeError("Modulo by zero");
}
return a % b;
});

export default {
apply,
count,
Expand Down Expand Up @@ -139,7 +146,7 @@ export default {
"-": numericBinaryOperator((a, b) => a - b),
"*": numericBinaryOperator((a, b) => a * b),
"/": divide,
"%": numericBinaryOperator((a, b) => a % b),
"%": modulo,
"||": or,
"&&": and,
"==": equal,
Expand Down
8 changes: 5 additions & 3 deletions js/src/builtins/mapkeys.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { pushRuntimeValueToStack } from "../stackManip";
import { BuiltinFunction } from "../types";
import { arity } from "../util";
import { arity, validateType } from "../util";
import { castToString, getProperties } from "../runtimeValues";

const mapkeys: BuiltinFunction = arity(2, (args, stack, exec) => {
const evaluated = exec(args[1], stack);
const evaluated = validateType("object", exec(args[1], stack));
const results = {};
getProperties(evaluated).forEach((key) => {
const newKey = castToString(exec(args[0], pushRuntimeValueToStack(key, stack)))
const newKey = castToString(
exec(args[0], pushRuntimeValueToStack(key, stack))
);
results[newKey] = evaluated[key];
});
return results;
Expand Down
4 changes: 2 additions & 2 deletions js/src/builtins/mapvalues.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { pushRuntimeValueToStack } from "../stackManip";
import { BuiltinFunction } from "../types";
import { arity } from "../util";
import { arity, validateType } from "../util";
import { getProperties } from "../runtimeValues";

const mapvalues: BuiltinFunction = arity(2, (args, stack, exec) => {
const evaluated = exec(args[1], stack);
const evaluated = validateType("object", exec(args[1], stack));
const results = {};
getProperties(evaluated).forEach((key) => {
results[key] = exec(
Expand Down
8 changes: 7 additions & 1 deletion js/src/runtimeValues.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,13 @@ export const castToString = (value: RuntimeValue): RuntimeValue => {
} else if (type === "regex" || typeof value === "function") {
throw new Error("Cannot cast type " + type + " to string");
} else {
return JSON.stringify(value);
return JSON.stringify(value, (_, value) => {
const type = getType(value);
if (type === "regex" || typeof value === "function") {
throw new Error("Cannot cast type " + type + " to string");
}
return value;
});
}
};

Expand Down
Loading