barefoot_js 0.40.1 → 0.41.0
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.
- checksums.yaml +4 -4
- data/README.md +15 -0
- data/lib/barefoot_js/backend/erb.rb +7 -2
- data/lib/barefoot_js/evaluator.rb +189 -47
- data/lib/barefoot_js/undefined.rb +106 -0
- data/lib/barefoot_js.rb +381 -203
- metadata +2 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 1facdd75f69a2a25ffc64d5b955ee60ca9e8761928448f2e70820e78384a5649
|
|
4
|
+
data.tar.gz: 30fd742f41f075dc877d4e7f530e5f963f539e285141c84d4205f8a9a4497efe
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 9290a45b87194f0572dd07675ac064e253d0641986841dce7bcb67e4ec147382d5976eec218f2fcd32e25b1f1abc406b4997102193b25c32471a54ca39a05dcf
|
|
7
|
+
data.tar.gz: dbf4857d8ea62f952f2fbf3823c3f6b2dd7f6cf00f106531b1787252fb447bff671196b86d625522fc436f5ba8fbde42a3f4fd132fe9de4bde253c2be7118b92
|
data/README.md
CHANGED
|
@@ -22,6 +22,21 @@ bf = BarefootJS::Context.new(backend)
|
|
|
22
22
|
|
|
23
23
|
Values are JSON-shaped Ruby data with symbol hash keys throughout (props, env hashes, array-of-hash records).
|
|
24
24
|
|
|
25
|
+
### `null` and `undefined`
|
|
26
|
+
|
|
27
|
+
Ruby's `nil` is JS `null`. JS `undefined` is `BarefootJS::UNDEF`, a frozen singleton. A props Hash already has the four states a JS prop has, so no wrapper type is needed:
|
|
28
|
+
|
|
29
|
+
| JS | Props Hash |
|
|
30
|
+
| --- | --- |
|
|
31
|
+
| prop not passed | key left out |
|
|
32
|
+
| `undefined` passed (`c={props.c}` with `c` unset) | `{ c: BarefootJS::UNDEF }` |
|
|
33
|
+
| `null` | `{ c: nil }` |
|
|
34
|
+
| a value | `{ c: 'x' }` |
|
|
35
|
+
|
|
36
|
+
A destructure default applies to the first two only. JSON decoded with `JSON.parse(..., symbolize_names: true)` gives `nil` for `null` and leaves a missing key out, which is what you want.
|
|
37
|
+
|
|
38
|
+
`UNDEF.nil?` is true and it prints as `''`, but Ruby cannot make it falsy: `if value` and `value || default` treat it as present. Test with `BarefootJS.undefined?`, `BarefootJS.null?` and `BarefootJS.nullish?` (`== null`) instead.
|
|
39
|
+
|
|
25
40
|
## Documentation
|
|
26
41
|
|
|
27
42
|
- [barefootjs.dev](https://barefootjs.dev/) — core documentation
|
|
@@ -85,10 +85,15 @@ module BarefootJS
|
|
|
85
85
|
end
|
|
86
86
|
|
|
87
87
|
# Render `<name>.erb` (relative to `path`) with `child_bf` bound as
|
|
88
|
-
# `bf` and `vars` (a Hash, symbol-keyed) bound as `v`.
|
|
88
|
+
# `bf` and `vars` (a Hash, symbol-keyed) bound as `v`. A template reads
|
|
89
|
+
# a var the host did not set as JS `undefined` (`BarefootJS::UNDEF`),
|
|
90
|
+
# through a copy of `vars` whose default is `UNDEF`; the host's own Hash
|
|
91
|
+
# is left as it is.
|
|
89
92
|
def render_named(name, child_bf, vars)
|
|
90
93
|
template = load_template(name)
|
|
91
|
-
|
|
94
|
+
bound = (vars || {}).dup
|
|
95
|
+
bound.default = BarefootJS::UNDEF
|
|
96
|
+
Renderer.new(child_bf, bound).render(template)
|
|
92
97
|
end
|
|
93
98
|
|
|
94
99
|
private
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require 'json'
|
|
4
|
+
require 'barefoot_js/undefined'
|
|
4
5
|
|
|
5
6
|
module BarefootJS
|
|
6
7
|
# Lightweight evaluator for the pure `ParsedExpr` subset, scoped to
|
|
@@ -32,6 +33,12 @@ module BarefootJS
|
|
|
32
33
|
# `object-literal` property keys) are plain Ruby Strings coming out of the
|
|
33
34
|
# parser; they are converted to Symbols at the point they touch a
|
|
34
35
|
# SYMBOL-keyed Hash (env or object value).
|
|
36
|
+
#
|
|
37
|
+
# JS `undefined` is `BarefootJS::UNDEF` (barefoot_js/undefined.rb); host
|
|
38
|
+
# `nil` is JS `null`. A missing field, an out-of-range or non-canonical
|
|
39
|
+
# index, the unbound `undefined` identifier, an omitted builtin argument
|
|
40
|
+
# and a `find` with no result read as `UNDEF` (spec/js-undefined-value.md,
|
|
41
|
+
# section 2).
|
|
35
42
|
module Evaluator
|
|
36
43
|
# Thrown when a node/operator/builtin/identifier is outside the subset.
|
|
37
44
|
class EvalUnsupported < StandardError; end
|
|
@@ -50,7 +57,12 @@ module BarefootJS
|
|
|
50
57
|
when 'identifier'
|
|
51
58
|
name = node[:name]
|
|
52
59
|
key = name.to_sym
|
|
53
|
-
|
|
60
|
+
unless env.key?(key)
|
|
61
|
+
# The unshadowed `undefined` identifier is the constant.
|
|
62
|
+
return UNDEF if name == 'undefined'
|
|
63
|
+
|
|
64
|
+
raise EvalUnsupported, "unbound identifier '#{name}'"
|
|
65
|
+
end
|
|
54
66
|
env[key]
|
|
55
67
|
when 'binary'
|
|
56
68
|
binary(node[:op], evaluate(node[:left], env), evaluate(node[:right], env))
|
|
@@ -62,7 +74,7 @@ module BarefootJS
|
|
|
62
74
|
case op
|
|
63
75
|
when '&&' then truthy?(left) ? evaluate(node[:right], env) : left
|
|
64
76
|
when '||' then truthy?(left) ? left : evaluate(node[:right], env)
|
|
65
|
-
else
|
|
77
|
+
else BarefootJS.nullish?(left) ? evaluate(node[:right], env) : left # '??' coalesces both nils
|
|
66
78
|
end
|
|
67
79
|
when 'conditional'
|
|
68
80
|
truthy?(evaluate(node[:test], env)) ? evaluate(node[:consequent], env) : evaluate(node[:alternate], env)
|
|
@@ -135,9 +147,11 @@ module BarefootJS
|
|
|
135
147
|
# `.join(sep?)` (#2094) -- a nested `.flatMap(p => p.tags.map(...))
|
|
136
148
|
# .join(...)` projection composes a `.join` on top of a nested
|
|
137
149
|
# `.map`, so it must be executable in the same evaluator subset as
|
|
138
|
-
# `.includes`.
|
|
139
|
-
#
|
|
140
|
-
|
|
150
|
+
# `.includes`. An omitted or `undefined` separator is "," (JS), a
|
|
151
|
+
# `null` one is ToString'd ("null"); a nil element joins as "" (not
|
|
152
|
+
# the string "null" / "undefined"). Mirrors Go's `evalJoin`.
|
|
153
|
+
sep_value = args.empty? ? UNDEF : evaluate(args[0], env)
|
|
154
|
+
sep = BarefootJS.undefined?(sep_value) ? ',' : to_string(sep_value)
|
|
141
155
|
array_join(evaluate(node[:object], env), sep)
|
|
142
156
|
else
|
|
143
157
|
# Every other array/string method (`slice`, `flat`, ...) is
|
|
@@ -164,7 +178,8 @@ module BarefootJS
|
|
|
164
178
|
# ---------------------------------------------------------------------
|
|
165
179
|
|
|
166
180
|
def to_number(v)
|
|
167
|
-
return 0 if
|
|
181
|
+
return 0 if BarefootJS.null?(v)
|
|
182
|
+
return Float::NAN if BarefootJS.undefined?(v)
|
|
168
183
|
return v ? 1 : 0 if v.is_a?(TrueClass) || v.is_a?(FalseClass)
|
|
169
184
|
return v if v.is_a?(Numeric)
|
|
170
185
|
if v.is_a?(String)
|
|
@@ -179,12 +194,20 @@ module BarefootJS
|
|
|
179
194
|
return v if v.is_a?(String)
|
|
180
195
|
return number_to_string(v) if v.is_a?(Numeric)
|
|
181
196
|
return v ? 'true' : 'false' if v.is_a?(TrueClass) || v.is_a?(FalseClass)
|
|
182
|
-
return 'null' if
|
|
197
|
+
return 'null' if BarefootJS.null?(v)
|
|
198
|
+
return 'undefined' if BarefootJS.undefined?(v)
|
|
199
|
+
# Array.prototype.toString is join(","): a nil element (`null` or
|
|
200
|
+
# `undefined`) prints as "", and a nested array flattens through its
|
|
201
|
+
# own join. A plain object prints as "[object Object]".
|
|
202
|
+
return v.map { |el| BarefootJS.nullish?(el) ? '' : to_string(el) }.join(',') if v.is_a?(Array)
|
|
203
|
+
return '[object Object]' if v.is_a?(Hash)
|
|
204
|
+
|
|
183
205
|
raise EvalUnsupported, "cannot coerce #{v.class} to string"
|
|
184
206
|
end
|
|
185
207
|
|
|
186
208
|
def truthy?(v)
|
|
187
|
-
|
|
209
|
+
# `UNDEF` is truthy under Ruby's own `if`: this is the JS test.
|
|
210
|
+
return false if BarefootJS.nullish?(v) || v.is_a?(FalseClass)
|
|
188
211
|
return true if v.is_a?(TrueClass)
|
|
189
212
|
if v.is_a?(Numeric)
|
|
190
213
|
f = v.to_f
|
|
@@ -199,13 +222,17 @@ module BarefootJS
|
|
|
199
222
|
# Number <-> String helpers
|
|
200
223
|
# ---------------------------------------------------------------------
|
|
201
224
|
|
|
202
|
-
|
|
225
|
+
# JS's NonDecimalIntegerLiteral: an unsigned `0x` / `0o` / `0b` integer.
|
|
226
|
+
RADIX_STRING_RES = { 16 => /\A0[xX][0-9a-fA-F]+\z/, 8 => /\A0[oO][0-7]+\z/, 2 => /\A0[bB][01]+\z/ }.freeze
|
|
203
227
|
NUMERIC_STRING_RE = /\A[+-]?(?:\d+\.?\d*|\.\d+)(?:[eE][+-]?\d+)?\z/
|
|
204
228
|
|
|
205
229
|
def parse_numeric_string(t)
|
|
206
230
|
return Float::INFINITY if t == 'Infinity' || t == '+Infinity'
|
|
207
231
|
return -Float::INFINITY if t == '-Infinity'
|
|
208
|
-
|
|
232
|
+
# JS rounds the mathematical value once to the nearest double, ties to
|
|
233
|
+
# even (#3824 step 3); Ruby's exact Integer#to_f does the same, and a
|
|
234
|
+
# value past the largest double is Infinity.
|
|
235
|
+
RADIX_STRING_RES.each { |base, re| return Integer(t, base).to_f if t =~ re }
|
|
209
236
|
|
|
210
237
|
if t =~ NUMERIC_STRING_RE
|
|
211
238
|
# Ruby's Float() rejects a trailing decimal point ("5.", "5.e3"),
|
|
@@ -309,11 +336,12 @@ module BarefootJS
|
|
|
309
336
|
def binary(op, l, r)
|
|
310
337
|
case op
|
|
311
338
|
when '+'
|
|
312
|
-
# JS `+`: string concatenation once either operand is a string
|
|
313
|
-
#
|
|
314
|
-
|
|
339
|
+
# JS `+`: string concatenation once either operand is a string after
|
|
340
|
+
# ToPrimitive (an array or object converts to its ToString), numeric
|
|
341
|
+
# addition otherwise.
|
|
342
|
+
return to_string(l) + to_string(r) if concatenates?(l) || concatenates?(r)
|
|
315
343
|
|
|
316
|
-
to_number(l)
|
|
344
|
+
add(to_number(l), to_number(r))
|
|
317
345
|
when '-' then to_number(l) - to_number(r)
|
|
318
346
|
when '*' then to_number(l) * to_number(r)
|
|
319
347
|
when '/' then divide(l, r)
|
|
@@ -325,14 +353,31 @@ module BarefootJS
|
|
|
325
353
|
raise EvalUnsupported, "binary operator '#{op}' is not in the evaluator subset"
|
|
326
354
|
end
|
|
327
355
|
end
|
|
328
|
-
|
|
356
|
+
|
|
357
|
+
# JS string order, by UTF-16 code unit. Ruby's String#<=> compares UTF-8
|
|
358
|
+
# bytes, which is code point order: the two differ where an astral
|
|
359
|
+
# character (whose leading surrogate is 0xD800-0xDBFF) meets a BMP
|
|
360
|
+
# character above the surrogates ("😀" < "。" is true in JS). Big-endian
|
|
361
|
+
# UTF-16 bytes compare in code-unit order.
|
|
362
|
+
def compare_utf16(l, r)
|
|
363
|
+
return l <=> r if l.ascii_only? && r.ascii_only?
|
|
364
|
+
|
|
365
|
+
utf16_be(l) <=> utf16_be(r)
|
|
366
|
+
end
|
|
367
|
+
|
|
368
|
+
def utf16_be(s)
|
|
369
|
+
s = s.dup.force_encoding(Encoding::UTF_8) if s.encoding == Encoding::BINARY
|
|
370
|
+
s.encode(Encoding::UTF_16BE, invalid: :replace, undef: :replace).b
|
|
371
|
+
end
|
|
372
|
+
|
|
373
|
+
private_class_method :compare_utf16, :utf16_be
|
|
329
374
|
|
|
330
375
|
def relational(op, l, r)
|
|
331
376
|
# JS Abstract Relational Comparison: both strings -> compare by code
|
|
332
377
|
# unit; otherwise coerce both to numbers (a NaN operand is false).
|
|
333
378
|
c =
|
|
334
379
|
if l.is_a?(String) && r.is_a?(String)
|
|
335
|
-
|
|
380
|
+
compare_utf16(l, r)
|
|
336
381
|
else
|
|
337
382
|
ln = to_number(l).to_f
|
|
338
383
|
rn = to_number(r).to_f
|
|
@@ -348,21 +393,21 @@ module BarefootJS
|
|
|
348
393
|
else false
|
|
349
394
|
end
|
|
350
395
|
end
|
|
351
|
-
private_class_method :relational
|
|
352
396
|
|
|
353
397
|
def strict_eq(l, r)
|
|
354
398
|
if non_primitive?(l) || non_primitive?(r)
|
|
355
399
|
raise EvalUnsupported, '=== on a non-primitive is not in the evaluator subset'
|
|
356
400
|
end
|
|
357
|
-
|
|
358
|
-
return
|
|
401
|
+
# `null` is only itself and `undefined` only itself.
|
|
402
|
+
return BarefootJS.null?(r) if BarefootJS.null?(l)
|
|
403
|
+
return BarefootJS.undefined?(r) if BarefootJS.undefined?(l)
|
|
404
|
+
return false if BarefootJS.nullish?(r)
|
|
359
405
|
return l == r if l.is_a?(Numeric) && r.is_a?(Numeric)
|
|
360
406
|
return l == r if boolean?(l) && boolean?(r)
|
|
361
407
|
return l == r if l.is_a?(String) && r.is_a?(String)
|
|
362
408
|
|
|
363
409
|
false
|
|
364
410
|
end
|
|
365
|
-
private_class_method :strict_eq
|
|
366
411
|
|
|
367
412
|
def non_primitive?(v)
|
|
368
413
|
v.is_a?(Array) || v.is_a?(Hash)
|
|
@@ -416,7 +461,7 @@ module BarefootJS
|
|
|
416
461
|
def array_join(obj, sep)
|
|
417
462
|
return '' unless obj.is_a?(Array)
|
|
418
463
|
|
|
419
|
-
obj.map { |el|
|
|
464
|
+
obj.map { |el| BarefootJS.nullish?(el) ? '' : to_string(el) }.join(sep)
|
|
420
465
|
end
|
|
421
466
|
private_class_method :array_join
|
|
422
467
|
|
|
@@ -428,12 +473,33 @@ module BarefootJS
|
|
|
428
473
|
def unary(op, v)
|
|
429
474
|
case op
|
|
430
475
|
when '!' then !truthy?(v)
|
|
431
|
-
when '-' then
|
|
476
|
+
when '-' then negate(to_number(v))
|
|
432
477
|
when '+' then to_number(v)
|
|
433
478
|
else raise EvalUnsupported, "unary operator '#{op}' is not in the evaluator subset"
|
|
434
479
|
end
|
|
435
480
|
end
|
|
436
|
-
|
|
481
|
+
|
|
482
|
+
def concatenates?(v)
|
|
483
|
+
v.is_a?(String) || v.is_a?(Array) || v.is_a?(Hash)
|
|
484
|
+
end
|
|
485
|
+
private_class_method :concatenates?
|
|
486
|
+
|
|
487
|
+
# add(a, b) -- JS numeric `+`, an IEEE double sum. Two Integers add
|
|
488
|
+
# exactly in Ruby; past 2^53 the exact sum is rounded once to the nearest
|
|
489
|
+
# double, which is what IEEE addition of the two (exactly held) operands
|
|
490
|
+
# gives (`2**53 + 1` is `2**53`).
|
|
491
|
+
def add(a, b)
|
|
492
|
+
sum = a + b
|
|
493
|
+
sum.is_a?(Integer) && sum.abs > 2**53 ? sum.to_f : sum
|
|
494
|
+
end
|
|
495
|
+
private_class_method :add
|
|
496
|
+
|
|
497
|
+
# negate(n) -- JS unary `-` over a number: an Integer zero has no sign, so
|
|
498
|
+
# a zero negates as a Float (`-0.0` from `0`, `0.0` from `-0.0`).
|
|
499
|
+
def negate(n)
|
|
500
|
+
n.zero? ? -n.to_f : -n
|
|
501
|
+
end
|
|
502
|
+
private_class_method :negate
|
|
437
503
|
|
|
438
504
|
# ---------------------------------------------------------------------
|
|
439
505
|
# Nested `.map`/`.filter` callback calls (#2094) -- the evaluator
|
|
@@ -526,6 +592,8 @@ module BarefootJS
|
|
|
526
592
|
private_class_method :math_round
|
|
527
593
|
|
|
528
594
|
def call_builtin(name, args)
|
|
595
|
+
# An omitted argument is JS `undefined`.
|
|
596
|
+
arg = ->(i) { i < args.length ? args[i] : UNDEF }
|
|
529
597
|
case name
|
|
530
598
|
when 'Math.max'
|
|
531
599
|
return -Float::INFINITY if args.empty?
|
|
@@ -541,17 +609,19 @@ module BarefootJS
|
|
|
541
609
|
return Float::NAN if nums.any?(&:nan?)
|
|
542
610
|
|
|
543
611
|
nums.min
|
|
544
|
-
when 'Math.abs' then to_number(
|
|
612
|
+
when 'Math.abs' then to_number(arg.call(0)).abs
|
|
545
613
|
when 'Math.floor'
|
|
546
|
-
n = to_number(
|
|
614
|
+
n = to_number(arg.call(0)).to_f
|
|
547
615
|
n.finite? ? n.floor : n
|
|
548
616
|
when 'Math.ceil'
|
|
549
|
-
n = to_number(
|
|
617
|
+
n = to_number(arg.call(0)).to_f
|
|
550
618
|
n.finite? ? n.ceil : n
|
|
551
|
-
when 'Math.round' then math_round(to_number(
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
when '
|
|
619
|
+
when 'Math.round' then math_round(to_number(arg.call(0)).to_f)
|
|
620
|
+
# `String()` / `Number()` with no argument are "" / 0; `String(undefined)`
|
|
621
|
+
# is "undefined" and `Number(undefined)` NaN.
|
|
622
|
+
when 'String' then args.empty? ? '' : to_string(args[0])
|
|
623
|
+
when 'Number' then args.empty? ? 0 : to_number(args[0])
|
|
624
|
+
when 'Boolean' then truthy?(arg.call(0))
|
|
555
625
|
else
|
|
556
626
|
raise EvalUnsupported, "builtin '#{name}' is not in the evaluator subset"
|
|
557
627
|
end
|
|
@@ -575,28 +645,97 @@ module BarefootJS
|
|
|
575
645
|
end
|
|
576
646
|
if obj.is_a?(Hash)
|
|
577
647
|
sym = key.to_sym
|
|
578
|
-
return obj.key?(sym) ? obj[sym] :
|
|
648
|
+
return obj.key?(sym) ? obj[sym] : UNDEF
|
|
579
649
|
end
|
|
580
|
-
raise EvalUnsupported, "cannot read property '#{key}' of #{obj
|
|
650
|
+
raise EvalUnsupported, "cannot read property '#{key}' of #{js_type_name(obj)}"
|
|
581
651
|
end
|
|
582
652
|
private_class_method :read_property
|
|
583
653
|
|
|
654
|
+
# JS `obj[index]` in a callback body: `index_value` over a receiver that
|
|
655
|
+
# can be indexed. Reading through null / undefined throws in JS, so it
|
|
656
|
+
# is outside the subset here.
|
|
584
657
|
def read_index(obj, index)
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
658
|
+
unless obj.is_a?(Array) || obj.is_a?(Hash) || obj.is_a?(String)
|
|
659
|
+
raise EvalUnsupported, "cannot index #{js_type_name(obj)}"
|
|
660
|
+
end
|
|
588
661
|
|
|
589
|
-
|
|
590
|
-
|
|
662
|
+
index_value(obj, index)
|
|
663
|
+
end
|
|
664
|
+
private_class_method :read_index
|
|
665
|
+
|
|
666
|
+
# The name a refusal message gives a receiver: JS's for the two nils.
|
|
667
|
+
def js_type_name(obj)
|
|
668
|
+
return 'null' if BarefootJS.null?(obj)
|
|
669
|
+
return 'undefined' if BarefootJS.undefined?(obj)
|
|
670
|
+
|
|
671
|
+
obj.class
|
|
672
|
+
end
|
|
673
|
+
private_class_method :js_type_name
|
|
674
|
+
|
|
675
|
+
# index_value(obj, index) -> JS `obj[index]`, shared with the template
|
|
676
|
+
# helper `bf.get` (which delegates here, so a template read and a
|
|
677
|
+
# callback read cannot disagree on a key). The key goes through
|
|
678
|
+
# ToPropertyKey (`to_string`: nil is "null", `-0` is "0", `1.0` is
|
|
679
|
+
# "1"). A Hash reads its own key (Symbol first: rows deserialize with
|
|
680
|
+
# `symbolize_names: true`). An Array, and a String (by UTF-16 code
|
|
681
|
+
# unit), are read only through a canonical array index: "1" reads
|
|
682
|
+
# element 1, while "01", "1e0", "-0", "1.1" and "-1" are ordinary
|
|
683
|
+
# property names (spec/js-primitive-semantics.md, "ToPropertyKey and
|
|
684
|
+
# index read"). A key the receiver does not have, and any read of a
|
|
685
|
+
# receiver with no properties, is `undefined`.
|
|
686
|
+
def index_value(obj, index)
|
|
687
|
+
key = property_key(index)
|
|
688
|
+
return UNDEF if key.nil?
|
|
689
|
+
|
|
690
|
+
case obj
|
|
691
|
+
when Hash
|
|
692
|
+
sym = key.to_sym
|
|
693
|
+
return obj[sym] if obj.key?(sym)
|
|
694
|
+
|
|
695
|
+
obj.key?(key) ? obj[key] : UNDEF
|
|
696
|
+
when Array, String
|
|
697
|
+
return UNDEF unless key.match?(/\A(?:0|[1-9][0-9]*)\z/)
|
|
591
698
|
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
699
|
+
i = key.to_i
|
|
700
|
+
return utf16_unit_at(obj, i) if obj.is_a?(String)
|
|
701
|
+
|
|
702
|
+
# An index past the end reads `undefined`. Checking before
|
|
703
|
+
# `Array#[]` also keeps an oversized key ("9223372036854775808",
|
|
704
|
+
# 1e20) from raising RangeError: a Bignum cannot be converted to a C
|
|
705
|
+
# long.
|
|
706
|
+
i < obj.length ? obj[i] : UNDEF
|
|
595
707
|
else
|
|
596
|
-
|
|
708
|
+
UNDEF
|
|
597
709
|
end
|
|
598
710
|
end
|
|
599
|
-
|
|
711
|
+
|
|
712
|
+
# JS ToPropertyKey of a primitive key (a Symbol a Ruby caller passes is
|
|
713
|
+
# its name); nil for a key outside the subset (an array or object key),
|
|
714
|
+
# which reads nothing.
|
|
715
|
+
def property_key(index)
|
|
716
|
+
return index.to_s if index.is_a?(Symbol)
|
|
717
|
+
|
|
718
|
+
to_string(index)
|
|
719
|
+
rescue EvalUnsupported
|
|
720
|
+
nil
|
|
721
|
+
end
|
|
722
|
+
private_class_method :property_key
|
|
723
|
+
|
|
724
|
+
# The UTF-16 code unit of `str` at index `i` (JS `str[i]`), or
|
|
725
|
+
# `undefined` past the end. One half of a surrogate pair reads as U+FFFD: a lone
|
|
726
|
+
# surrogate cannot be written out as UTF-8
|
|
727
|
+
# (spec/js-primitive-semantics.md, "Out of scope").
|
|
728
|
+
def utf16_unit_at(str, i)
|
|
729
|
+
pos = 0
|
|
730
|
+
str.each_char do |c|
|
|
731
|
+
width = c.ord > 0xFFFF ? 2 : 1
|
|
732
|
+
return width == 1 ? c : "\uFFFD" if i < pos + width
|
|
733
|
+
|
|
734
|
+
pos += width
|
|
735
|
+
end
|
|
736
|
+
UNDEF
|
|
737
|
+
end
|
|
738
|
+
private_class_method :utf16_unit_at
|
|
600
739
|
|
|
601
740
|
# ---------------------------------------------------------------------
|
|
602
741
|
# Evaluator-driven higher-order folds -- the runtime half `bf.rb` calls
|
|
@@ -639,7 +778,10 @@ module BarefootJS
|
|
|
639
778
|
env = base_env ? base_env.dup : {}
|
|
640
779
|
a_key = param_a.to_sym
|
|
641
780
|
b_key = param_b.to_sym
|
|
642
|
-
|
|
781
|
+
# JS `Array.prototype.sort` moves every `undefined` element to the end
|
|
782
|
+
# without handing it to the comparator.
|
|
783
|
+
defined, tail = items.partition { |item| !BarefootJS.undefined?(item) }
|
|
784
|
+
decorated = defined.each_with_index.map { |item, i| [i, item] }
|
|
643
785
|
sorted = decorated.sort do |x, y|
|
|
644
786
|
env[a_key] = x[1]
|
|
645
787
|
env[b_key] = y[1]
|
|
@@ -647,7 +789,7 @@ module BarefootJS
|
|
|
647
789
|
sign = c.nan? ? 0 : (c <=> 0)
|
|
648
790
|
sign.zero? ? (x[0] <=> y[0]) : sign
|
|
649
791
|
end
|
|
650
|
-
sorted.map { |pair| pair[1] }
|
|
792
|
+
sorted.map { |pair| pair[1] } + tail
|
|
651
793
|
end
|
|
652
794
|
|
|
653
795
|
def sort_by_json(items, cmp_json, param_a, param_b, base_env = nil)
|
|
@@ -691,8 +833,8 @@ module BarefootJS
|
|
|
691
833
|
end
|
|
692
834
|
end
|
|
693
835
|
|
|
694
|
-
# find -- first matching element, or
|
|
695
|
-
# the end (findLast).
|
|
836
|
+
# find -- first matching element, or `undefined`. `forward` false
|
|
837
|
+
# searches from the end (findLast).
|
|
696
838
|
def find(items, pred, param, forward = true, base_env = nil)
|
|
697
839
|
arr = items.is_a?(Array) ? items : []
|
|
698
840
|
arr = arr.reverse unless forward
|
|
@@ -702,7 +844,7 @@ module BarefootJS
|
|
|
702
844
|
env[key] = item
|
|
703
845
|
return item if truthy?(evaluate(pred, env))
|
|
704
846
|
end
|
|
705
|
-
|
|
847
|
+
UNDEF
|
|
706
848
|
end
|
|
707
849
|
|
|
708
850
|
# find_index -- index of the first matching element, or -1. `forward`
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module BarefootJS
|
|
4
|
+
# JS `undefined` (spec/js-undefined-value.md). Host `nil` is JS `null`; JS
|
|
5
|
+
# `undefined` is one frozen singleton, `BarefootJS::UNDEF`, created only
|
|
6
|
+
# where JS reads one: a missing key, an out-of-range index, `.at()` /
|
|
7
|
+
# `.find()` with no result, `?.` over a nullish receiver, the `undefined`
|
|
8
|
+
# identifier (`bf.undefined` in a template), an omitted declared prop. No
|
|
9
|
+
# decoded data is walked to convert anything.
|
|
10
|
+
#
|
|
11
|
+
# Ruby cannot make an object falsy, so `UNDEF` is truthy under Ruby's own
|
|
12
|
+
# `if` / `||` / `&&` / `unless`. Every JS truthiness test in a compiled
|
|
13
|
+
# template and in this runtime goes through `Evaluator.truthy?`
|
|
14
|
+
# (`bf.truthy?`), which reads it as falsy. `nil?` answers true, so a
|
|
15
|
+
# native `x.nil?` in a template is the JS nullish test (`x == null`); the
|
|
16
|
+
# runtime never relies on it and tests with `BarefootJS.undefined?` /
|
|
17
|
+
# `null?` / `nullish?` instead. `&.` does not consult `nil?`: it calls
|
|
18
|
+
# through on `UNDEF`, which is why a template spells `o?.k` as
|
|
19
|
+
# `bf.get_opt(o, :k)`.
|
|
20
|
+
#
|
|
21
|
+
# It prints as `''` (`to_s`), as JS renders `undefined` in a text or
|
|
22
|
+
# attribute position, and reading any key of it gives itself (`[]`), so a
|
|
23
|
+
# chained read past it stays `undefined` instead of raising. `to_json`
|
|
24
|
+
# gives `null` for a host encoder; the runtime's own JSON walk follows
|
|
25
|
+
# `JSON.stringify` instead (a member holding it is dropped).
|
|
26
|
+
class Undefined
|
|
27
|
+
def nil?
|
|
28
|
+
true
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def to_s
|
|
32
|
+
''
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def inspect
|
|
36
|
+
'undefined'
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def [](*)
|
|
40
|
+
self
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def to_json(*)
|
|
44
|
+
'null'
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
# The singleton survives a copy (a host's `deep_dup`, `Marshal`-free
|
|
48
|
+
# cloning), so identity tests keep holding.
|
|
49
|
+
def dup
|
|
50
|
+
self
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def clone(*)
|
|
54
|
+
self
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
UNDEF = Undefined.new.freeze
|
|
59
|
+
Undefined.private_class_method :new
|
|
60
|
+
|
|
61
|
+
module_function
|
|
62
|
+
|
|
63
|
+
# JS `v === undefined`.
|
|
64
|
+
def undefined?(value)
|
|
65
|
+
Undefined === value
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
# JS `v === null`: host `nil` (strictly: `UNDEF.nil?` is true too).
|
|
69
|
+
def null?(value)
|
|
70
|
+
NilClass === value
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
# JS `v == null`: `null` or `undefined`.
|
|
74
|
+
def nullish?(value)
|
|
75
|
+
NilClass === value || Undefined === value
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
# Whether a JSON-decoded value (symbol or string keys) is the
|
|
79
|
+
# `{"$undef": true}` sentinel, the manifest's (and the golden vectors')
|
|
80
|
+
# spelling of JS `undefined` (spec/js-undefined-value.md, "JSON encoding
|
|
81
|
+
# for vectors").
|
|
82
|
+
def undefined_sentinel?(value)
|
|
83
|
+
value.is_a?(Hash) && value.size == 1 && (value[:$undef] == true || value['$undef'] == true)
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
# `value` with every sentinel, at any depth, replaced by `UNDEF`; inside a
|
|
87
|
+
# hash it is a present key holding `undefined`. Every other hash key that
|
|
88
|
+
# begins with `$$` loses one leading `$`: the manifest encoder escapes a
|
|
89
|
+
# seed's own `$`-prefixed keys that way, so data shaped like the sentinel
|
|
90
|
+
# stays data.
|
|
91
|
+
def decode_undefined(value)
|
|
92
|
+
return UNDEF if undefined_sentinel?(value)
|
|
93
|
+
return value.map { |v| decode_undefined(v) } if value.is_a?(Array)
|
|
94
|
+
return value.to_h { |k, v| [unescape_key(k), decode_undefined(v)] } if value.is_a?(Hash)
|
|
95
|
+
|
|
96
|
+
value
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
# A `$$`-prefixed key (symbol or string) with one leading `$` removed.
|
|
100
|
+
def unescape_key(key)
|
|
101
|
+
name = key.to_s
|
|
102
|
+
return key unless name.start_with?('$$')
|
|
103
|
+
|
|
104
|
+
key.is_a?(Symbol) ? name[1..].to_sym : name[1..]
|
|
105
|
+
end
|
|
106
|
+
end
|