Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow complex lengths for temp_array #150

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
35 changes: 35 additions & 0 deletions tests/scenetest.js
Original file line number Diff line number Diff line change
Expand Up @@ -2349,6 +2349,28 @@ test("tempArrayExplicitAndComplex", function() {
doh.is(2, scene.temps.t_arr_count, "scene.temps.t_arr_count");
doh.is("undefined", typeof scene.temps.t_arr_3, "scene.temps.t_arr_3");
});
test("tempArrayExpressionVarLength", function() {
var scene = new Scene();
scene.loadLines(dedent`
*temp count 10
*temp_array t_arr count 0`);
scene.execute();
doh.is("undefined", typeof scene.temps.t_arr_0, "scene.temps.t_arr_0");
doh.is(0, scene.temps.t_arr_1, "scene.temps.t_arr_1");
doh.is(10, scene.temps.t_arr_count, "scene.temps.t_arr_count");
});
test("tempArrayExpressionLength", function() {
var scene = new Scene();
scene.name = "startup";
scene.loadLines(dedent`
*create_array global 2 0
*temp arr_ref "global"
*temp_array t_arr {arr_ref&"_count"} 0`);
scene.execute();
doh.is("undefined", typeof scene.temps.t_arr_0, "scene.temps.t_arr_0");
doh.is(0, scene.temps.t_arr_1, "scene.temps.t_arr_1");
doh.is(2, scene.temps.t_arr_count, "scene.temps.t_arr_count");
});
test("tempArrayEmpty", function() {
var scene = new Scene();
scene.loadLines("*temp_array t_arr 2");
Expand All @@ -2374,6 +2396,19 @@ test("errorTempArrayValues", function() {
scene.loadLines("*temp_array foo 5 1 2 3 4 5 6");
doh.assertError(Error, scene, "execute", null, "Expected 1 or 5 values for array foo not 6");
});
test("errorTempArrayComplexLengthZero", function() {
var scene = new Scene();
scene.loadLines(dedent`
*temp size 0
*temp_array t_arr size 0`);
doh.assertError(Error, scene, "execute", null, "Complex temp_array lengths should only evaluate to a number greater than 0");
});
test("errorCreateArrayComplexLength", function() {
var scene = new Scene();
scene.name = "startup";
scene.loadLines("*create_array foo {myexpr} 2");
doh.assertError(Error, scene, "execute", null, "Create instructions shouldn't allow complex lengths");
});
test("deleteCreateArray", function() {
var scene = new Scene();
scene.name = "startup";
Expand Down
16 changes: 14 additions & 2 deletions web/scene.js
Original file line number Diff line number Diff line change
Expand Up @@ -1586,9 +1586,21 @@ Scene.prototype.defineArray = function defineArray(command, line) {
if (!stack.length) {
throw new Error(this.lineMsg() + "Invalid " + command + "_array instruction, missing length: " + line);
}
var length = Number(stack.shift().value);

var length_token = stack[0];
var length;
if (length_token.name === "NUMBER") {
length = Number(length_token.value);
stack.shift();
} else {
if (command === "create") {
throw new Error(this.lineMsg() + "Invalid create_array instruction, length must be a number: " + line);
}
length = Number(this.evaluateValueToken(stack.shift(), stack));
}

if (length !== Math.floor(length) || length < 1) {
throw new Error(this.lineMsg() + "Invalid " + command + "_array instruction, length should be a whole number greater than 0: " + line);
throw new Error(this.lineMsg() + `Invalid ${command}_array instruction, got a length of ${length} but wanted a length greater than 0: ${line}`);
}

var token;
Expand Down