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

Add annotation for table-from-raw-array(), checking arg is indeed a raw array of rows #1759

Merged
merged 4 commits into from
Dec 6, 2024
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
9 changes: 7 additions & 2 deletions src/arr/trove/tables.arr
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
provide:
* hiding (is-kv-pairs),
* hiding (is-kv-pairs, is-raw-array-of-rows),
type *
end

import global as G
include from G: raw-array-duplicate end
include lists
import table as T

type Reducer<Acc, InVal, OutVal> = {
one :: (InVal -> {Acc; OutVal}),
Expand Down Expand Up @@ -88,7 +89,11 @@ fun empty-table(col-names :: List<String>) -> Table:
end
end

fun table-from-raw-array(arr):
fun is-raw-array-of-rows(ra :: RawArray<Any>) -> Boolean:
raw-array-fold(lam(base, elt, _): base and is-row(elt) end, true, ra, 0)
end

fun table-from-raw-array(arr :: T.RawArrayOfRows) -> Table:
col-names = raw-array-get(arr, 0).get-column-names()
with-cols = empty-table(col-names)
for raw-array-fold(t from with-cols, r from arr, _ from 0):
Expand Down
2 changes: 1 addition & 1 deletion src/js/base/post-load-hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ define("pyret-base/js/post-load-hooks", function() {
runtime["checkEQ"] = runtime.makeCheckType(ffi.isEqualityResult, "EqualityResult");
},
"builtin://table": function(table) {
table = table.jsmod;
table = runtime.getField(runtime.getField(table, "provide-plus-types"), "internal");
runtime["makeTable"] = table.makeTable;
runtime["makeRow"] = table.makeRow;
runtime["makeRowFromArray"] = table.makeRowFromArray;
Expand Down
4 changes: 3 additions & 1 deletion src/js/trove/reactors.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,9 @@
i += 1;
return ans;
});
return tables.makeTable(["tick", "state"], rows);
console.log('tables =', tables);
return gf(tables, "internal").makeTable(["tick", "state"], rows);
// return runtime.makeTable(["tick", "state"], rows);
}
else {
runtime.ffi.throwMessageException("Tried to get trace of a reactor that isn't tracing; try calling start-trace() first");
Expand Down
34 changes: 28 additions & 6 deletions src/js/trove/table.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
nativeRequires: [
"pyret-base/js/type-util"
],
provides: {},
provides: {
types: {
'RawArrayOfRows': 'tany'
}
},
theModule: function(runtime, namespace, uri, VSlib, EQlib, ffi, t) {
var get = runtime.getField;

Expand All @@ -23,6 +27,20 @@
var brandRow = runtime.namedBrander("row", ["table: row brander"]);
var annRow = runtime.makeBranderAnn(brandRow, "Row");

var ann = function(name, pred) {
return runtime.makePrimitiveAnn(name, pred);
};

function isRawArrayOfRows(raor) {
if (!Array.isArray(raor)) return false;
for (let i = 0; i < raor.length; i++) {
if (!runtime.hasBrand(raor[i], brandRow._brand)) return false;
}
return true;
}

var annRawArrayOfRows = ann("RawArrayOfRows", isRawArrayOfRows);

var rowGetValue = runtime.makeMethod1(function(self, arg) {
ffi.checkArity(2, arguments, "get-value", true);
runtime.checkArgsInternal2("tables", "get-value",
Expand Down Expand Up @@ -820,16 +838,20 @@
}));
}

return runtime.makeJSModuleReturn({
TableAnn : annTable,
RowAnn : annRow,
var internal = {
makeTable: makeTable,
makeRow: makeRow,
makeRowFromArray: makeRowFromArray,
openTable: openTable,
isTable: isTable,
isRow: isRow
},
{});
};
var types = {
Table: annTable,
Row: annRow,
RawArrayOfRows: annRawArrayOfRows
};
var values = {};
return runtime.makeModuleReturn(values, types, internal);
}
})
18 changes: 17 additions & 1 deletion tests/pyret/tests/test-tables.arr
Original file line number Diff line number Diff line change
Expand Up @@ -687,8 +687,24 @@ check "table-from-rows":
t4 = table-from-rows.make(raw-array-from-list(new-row-list))

nothing does-not-raise # Dummy test to avoid well-formedness errors in the previous row
end

[table-from-rows:
[list:
[raw-row: {"A"; 5}, {"B"; 7}, {"C"; 8}],
[raw-row: {"A"; 1}, {"B"; 2}, {"C"; 3}]]]
raises "RawArrayOfRows"

[table-from-rows:
1, [raw-row: {"A"; 1}, {"B"; 2}],
false, [raw-row: {"A"; 3}, {"B"; 4}],
"non-row string", [raw-row: {"A"; 5}, {"B"; 6}]]
raises "RawArrayOfRows"

[table-from-rows:
[raw-row: 5, {"B"; 7}, {"C"; 8}],
[raw-row: {"A"; 1}, {"B"; 2}, {"C"; 3}]]
raises "KeyValPair"
end

table-from-column = TS.table-from-column
table-from-columns = TS.table-from-columns
Expand Down