@weborigami/language 0.0.65-beta.2 → 0.0.66-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -5,20 +5,19 @@ import { evaluate } from "./internal.js";
5
5
  /**
6
6
  * Given parsed Origami code, return a function that executes that code.
7
7
  *
8
- * @param {import("../../index.js").ParseResult} parsed - parsed Origami expression
8
+ * @param {import("../../index.js").Code} code - parsed Origami expression
9
9
  * @param {string} [name] - optional name of the function
10
10
  */
11
- export function createExpressionFunction(parsed, name) {
11
+ export function createExpressionFunction(code, name) {
12
12
  /** @this {AsyncTree|null} */
13
13
  async function fn() {
14
- return evaluate.call(this, parsed);
14
+ return evaluate.call(this, code);
15
15
  }
16
16
  if (name) {
17
17
  Object.defineProperty(fn, "name", { value: name });
18
18
  }
19
- fn.code = parsed;
20
- fn.toString = () =>
21
- parsed instanceof Array ? parsed.location.source.text : parsed;
19
+ fn.code = code;
20
+ fn.toString = () => code.location.source.text;
22
21
  return fn;
23
22
  }
24
23
 
@@ -1,4 +1,7 @@
1
1
  // Text we look for in an error stack to guess whether a given line represents a
2
+
3
+ import codeFragment from "./codeFragment.js";
4
+
2
5
  // function in the Origami source code.
3
6
  const origamiSourceSignals = [
4
7
  "async-tree/src/",
@@ -35,12 +38,9 @@ export default function formatError(error) {
35
38
  // Add location
36
39
  let location = /** @type {any} */ (error).location;
37
40
  if (location) {
38
- let { source, start, end } = location;
39
- let fragment = source.text.slice(start.offset, end.offset);
40
- if (fragment.length === 0) {
41
- // Use entire source.
42
- fragment = source.text;
43
- }
41
+ const fragment = codeFragment(location);
42
+ let { source, start } = location;
43
+
44
44
  message += `\nevaluating: ${fragment}`;
45
45
  if (typeof source === "object" && source.url) {
46
46
  message += `\n at ${source.url.href}:${start.line}:${start.column}`;
@@ -222,7 +222,7 @@ export function lambda(parameters, code) {
222
222
  // We set the `length` property on the function so that Tree.traverseOrThrow()
223
223
  // will correctly identify how many parameters it wants. This is unorthodox
224
224
  // but doesn't appear to affect other behavior.
225
- const fnLength = Object.keys(parameters).length;
225
+ const fnLength = parameters.length;
226
226
  Object.defineProperty(invoke, "length", {
227
227
  value: fnLength,
228
228
  });
@@ -282,6 +282,14 @@ export function spread(...args) {
282
282
  }
283
283
  spread.toString = () => "«ops.spread»";
284
284
 
285
+ /**
286
+ * Return a primitive value
287
+ */
288
+ export async function primitive(value) {
289
+ return value;
290
+ }
291
+ primitive.toString = () => "«ops.primitive»";
292
+
285
293
  /**
286
294
  * Traverse a path of keys through a tree.
287
295
  */