@mapwhit/tilerenderer 0.49.0 → 0.51.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.
Files changed (147) hide show
  1. package/build/min/package.json +1 -1
  2. package/build/min/src/shaders/.dir +0 -0
  3. package/build/min/src/shaders/_prelude.fragment.glsl.js +14 -0
  4. package/build/min/src/shaders/_prelude.vertex.glsl.js +14 -0
  5. package/build/min/src/shaders/background.fragment.glsl.js +5 -0
  6. package/build/min/src/shaders/background.vertex.glsl.js +1 -0
  7. package/build/min/src/shaders/background_pattern.fragment.glsl.js +5 -0
  8. package/build/min/src/shaders/background_pattern.vertex.glsl.js +1 -0
  9. package/build/min/src/shaders/circle.fragment.glsl.js +20 -0
  10. package/build/min/src/shaders/circle.vertex.glsl.js +17 -0
  11. package/build/min/src/shaders/clipping_mask.fragment.glsl.js +1 -0
  12. package/build/min/src/shaders/clipping_mask.vertex.glsl.js +1 -0
  13. package/build/min/src/shaders/collision_box.fragment.glsl.js +1 -0
  14. package/build/min/src/shaders/collision_box.vertex.glsl.js +1 -0
  15. package/build/min/src/shaders/collision_circle.fragment.glsl.js +1 -0
  16. package/build/min/src/shaders/collision_circle.vertex.glsl.js +1 -0
  17. package/build/min/src/shaders/debug.fragment.glsl.js +1 -0
  18. package/build/min/src/shaders/debug.vertex.glsl.js +1 -0
  19. package/build/min/src/shaders/fill.fragment.glsl.js +10 -0
  20. package/build/min/src/shaders/fill.vertex.glsl.js +7 -0
  21. package/build/min/src/shaders/fill_extrusion.fragment.glsl.js +5 -0
  22. package/build/min/src/shaders/fill_extrusion.fragment.glsl.txt +1 -9
  23. package/build/min/src/shaders/fill_extrusion.vertex.glsl.js +9 -0
  24. package/build/min/src/shaders/fill_extrusion.vertex.glsl.txt +4 -4
  25. package/build/min/src/shaders/fill_extrusion_pattern.fragment.glsl.js +15 -0
  26. package/build/min/src/shaders/fill_extrusion_pattern.vertex.glsl.js +11 -0
  27. package/build/min/src/shaders/fill_outline.fragment.glsl.js +10 -0
  28. package/build/min/src/shaders/fill_outline.vertex.glsl.js +7 -0
  29. package/build/min/src/shaders/fill_outline_pattern.fragment.glsl.js +13 -0
  30. package/build/min/src/shaders/fill_outline_pattern.vertex.glsl.js +9 -0
  31. package/build/min/src/shaders/fill_pattern.fragment.glsl.js +13 -0
  32. package/build/min/src/shaders/fill_pattern.vertex.glsl.js +9 -0
  33. package/build/min/src/shaders/heatmap.fragment.glsl.js +10 -0
  34. package/build/min/src/shaders/heatmap.vertex.glsl.js +8 -0
  35. package/build/min/src/shaders/heatmap_texture.fragment.glsl.js +5 -0
  36. package/build/min/src/shaders/heatmap_texture.vertex.glsl.js +1 -0
  37. package/build/min/src/shaders/hillshade.fragment.glsl.js +7 -0
  38. package/build/min/src/shaders/hillshade.vertex.glsl.js +1 -0
  39. package/build/min/src/shaders/hillshade_prepare.fragment.glsl.js +8 -0
  40. package/build/min/src/shaders/hillshade_prepare.vertex.glsl.js +1 -0
  41. package/build/min/src/shaders/line.fragment.glsl.js +12 -0
  42. package/build/min/src/shaders/line.vertex.glsl.js +17 -0
  43. package/build/min/src/shaders/line_gradient.fragment.glsl.js +10 -0
  44. package/build/min/src/shaders/line_gradient.vertex.glsl.js +16 -0
  45. package/build/min/src/shaders/line_pattern.fragment.glsl.js +15 -0
  46. package/build/min/src/shaders/line_pattern.vertex.glsl.js +20 -0
  47. package/build/min/src/shaders/line_sdf.fragment.glsl.js +17 -0
  48. package/build/min/src/shaders/line_sdf.vertex.glsl.js +20 -0
  49. package/build/min/src/shaders/raster.fragment.glsl.js +5 -0
  50. package/build/min/src/shaders/raster.vertex.glsl.js +1 -0
  51. package/build/min/src/shaders/symbol_icon.fragment.glsl.js +9 -0
  52. package/build/min/src/shaders/symbol_icon.vertex.glsl.js +5 -0
  53. package/build/min/src/shaders/symbol_sdf.fragment.glsl.js +19 -0
  54. package/build/min/src/shaders/symbol_sdf.vertex.glsl.js +13 -0
  55. package/package.json +7 -7
  56. package/src/data/bucket/fill_bucket.js +1 -1
  57. package/src/data/bucket/fill_extrusion_bucket.js +2 -2
  58. package/src/data/bucket/line_bucket.js +1 -1
  59. package/src/data/bucket/symbol_bucket.js +16 -15
  60. package/src/data/feature_index.js +1 -1
  61. package/src/data/program_configuration.js +1 -1
  62. package/src/gl/color_mode.js +1 -1
  63. package/src/gl/value.js +1 -1
  64. package/src/render/draw_debug.js +1 -1
  65. package/src/render/draw_fill.js +1 -1
  66. package/src/render/draw_heatmap.js +1 -1
  67. package/src/render/glyph_manager.js +6 -130
  68. package/src/render/painter.js +1 -1
  69. package/src/render/uniform_binding.js +1 -1
  70. package/src/shaders/fill_extrusion.fragment.glsl +0 -7
  71. package/src/shaders/fill_extrusion.vertex.glsl +4 -4
  72. package/src/shaders/index.js +50 -50
  73. package/src/source/geojson_worker_source.js +4 -4
  74. package/src/source/geojson_wrapper.js +34 -26
  75. package/src/source/resources/glyphs.js +73 -0
  76. package/src/source/resources/images.js +68 -0
  77. package/src/source/resources/index.js +22 -0
  78. package/src/source/vector_tile_worker_source.js +5 -5
  79. package/src/source/worker.js +6 -5
  80. package/src/source/worker_tile.js +4 -4
  81. package/src/style/properties.js +1 -2
  82. package/src/style/style.js +3 -3
  83. package/src/style/style_layer/symbol_style_layer.js +1 -1
  84. package/src/style/style_layer.js +1 -1
  85. package/src/style-spec/feature_filter/index.js +43 -34
  86. package/src/style-spec/group_by_layout.js +10 -32
  87. package/src/style-spec/reference/v8.json +1 -1
  88. package/src/symbol/mergelines.js +1 -3
  89. package/src/symbol/placement.js +21 -2
  90. package/src/symbol/shaping.js +9 -18
  91. package/src/symbol/symbol_layout.js +1 -2
  92. package/src/symbol/symbol_size.js +1 -1
  93. package/src/symbol/transform_text.js +4 -8
  94. package/src/ui/map.js +11 -39
  95. package/src/util/browser.js +3 -18
  96. package/src/util/classify_rings.js +1 -1
  97. package/src/util/dom.js +0 -88
  98. package/src/util/find_pole_of_inaccessibility.js +2 -2
  99. package/src/util/web_worker_transfer.js +4 -4
  100. package/src/style/load_glyph_range.js +0 -17
  101. package/src/style-spec/expression/compound_expression.js +0 -132
  102. package/src/style-spec/expression/definitions/assertion.js +0 -116
  103. package/src/style-spec/expression/definitions/at.js +0 -57
  104. package/src/style-spec/expression/definitions/case.js +0 -73
  105. package/src/style-spec/expression/definitions/coalesce.js +0 -68
  106. package/src/style-spec/expression/definitions/coercion.js +0 -109
  107. package/src/style-spec/expression/definitions/collator.js +0 -102
  108. package/src/style-spec/expression/definitions/comparison.js +0 -193
  109. package/src/style-spec/expression/definitions/formatted.js +0 -123
  110. package/src/style-spec/expression/definitions/index.js +0 -356
  111. package/src/style-spec/expression/definitions/interpolate.js +0 -245
  112. package/src/style-spec/expression/definitions/length.js +0 -54
  113. package/src/style-spec/expression/definitions/let.js +0 -60
  114. package/src/style-spec/expression/definitions/literal.js +0 -69
  115. package/src/style-spec/expression/definitions/match.js +0 -142
  116. package/src/style-spec/expression/definitions/step.js +0 -116
  117. package/src/style-spec/expression/definitions/var.js +0 -38
  118. package/src/style-spec/expression/evaluation_context.js +0 -35
  119. package/src/style-spec/expression/index.js +0 -329
  120. package/src/style-spec/expression/is_constant.js +0 -63
  121. package/src/style-spec/expression/parsing_context.js +0 -212
  122. package/src/style-spec/expression/parsing_error.js +0 -9
  123. package/src/style-spec/expression/runtime_error.js +0 -12
  124. package/src/style-spec/expression/scope.js +0 -34
  125. package/src/style-spec/expression/stops.js +0 -37
  126. package/src/style-spec/expression/types.js +0 -88
  127. package/src/style-spec/expression/values.js +0 -126
  128. package/src/style-spec/function/convert.js +0 -234
  129. package/src/style-spec/function/index.js +0 -299
  130. package/src/style-spec/util/color.js +0 -73
  131. package/src/style-spec/util/color_spaces.js +0 -128
  132. package/src/style-spec/util/get_type.js +0 -18
  133. package/src/style-spec/util/interpolate.js +0 -21
  134. package/src/style-spec/util/properties.js +0 -17
  135. package/src/style-spec/util/result.js +0 -19
  136. package/src/ui/anchor.js +0 -24
  137. package/src/ui/bind_handlers.js +0 -199
  138. package/src/ui/events.js +0 -210
  139. package/src/ui/handler/box_zoom.js +0 -151
  140. package/src/ui/handler/dblclick_zoom.js +0 -91
  141. package/src/ui/handler/drag_pan.js +0 -285
  142. package/src/ui/handler/drag_rotate.js +0 -290
  143. package/src/ui/handler/frame.js +0 -28
  144. package/src/ui/handler/inertia.js +0 -45
  145. package/src/ui/handler/keyboard.js +0 -148
  146. package/src/ui/handler/scroll_zoom.js +0 -284
  147. package/src/ui/handler/touch_zoom_rotate.js +0 -263
@@ -1,57 +0,0 @@
1
- const { array, ValueType, NumberType } = require('../types');
2
-
3
- const RuntimeError = require('../runtime_error');
4
-
5
- class At {
6
- constructor(type, index, input) {
7
- this.type = type;
8
- this.index = index;
9
- this.input = input;
10
- }
11
-
12
- static parse(args, context) {
13
- if (args.length !== 3) return context.error(`Expected 2 arguments, but found ${args.length - 1} instead.`);
14
-
15
- const index = context.parse(args[1], 1, NumberType);
16
- const input = context.parse(args[2], 2, array(context.expectedType || ValueType));
17
-
18
- if (!index || !input) return null;
19
-
20
- const t = input.type;
21
- return new At(t.itemType, index, input);
22
- }
23
-
24
- evaluate(ctx) {
25
- const index = this.index.evaluate(ctx);
26
- const array = this.input.evaluate(ctx);
27
-
28
- if (index < 0) {
29
- throw new RuntimeError(`Array index out of bounds: ${index} < 0.`);
30
- }
31
-
32
- if (index >= array.length) {
33
- throw new RuntimeError(`Array index out of bounds: ${index} > ${array.length - 1}.`);
34
- }
35
-
36
- if (index !== Math.floor(index)) {
37
- throw new RuntimeError(`Array index must be an integer, but found ${index} instead.`);
38
- }
39
-
40
- return array[index];
41
- }
42
-
43
- eachChild(fn) {
44
- fn(this.index);
45
- fn(this.input);
46
- }
47
-
48
- possibleOutputs() {
49
- return [undefined];
50
- }
51
-
52
- serialize() {
53
- return ['at', this.index.serialize(), this.input.serialize()];
54
- }
55
- }
56
-
57
- module.exports = At;
@@ -1,73 +0,0 @@
1
- const assert = require('assert');
2
-
3
- const { BooleanType } = require('../types');
4
-
5
- class Case {
6
- constructor(type, branches, otherwise) {
7
- this.type = type;
8
- this.branches = branches;
9
- this.otherwise = otherwise;
10
- }
11
-
12
- static parse(args, context) {
13
- if (args.length < 4) return context.error(`Expected at least 3 arguments, but found only ${args.length - 1}.`);
14
- if (args.length % 2 !== 0) return context.error('Expected an odd number of arguments.');
15
-
16
- let outputType;
17
- if (context.expectedType && context.expectedType.kind !== 'value') {
18
- outputType = context.expectedType;
19
- }
20
-
21
- const branches = [];
22
- for (let i = 1; i < args.length - 1; i += 2) {
23
- const test = context.parse(args[i], i, BooleanType);
24
- if (!test) return null;
25
-
26
- const result = context.parse(args[i + 1], i + 1, outputType);
27
- if (!result) return null;
28
-
29
- branches.push([test, result]);
30
-
31
- outputType = outputType || result.type;
32
- }
33
-
34
- const otherwise = context.parse(args[args.length - 1], args.length - 1, outputType);
35
- if (!otherwise) return null;
36
-
37
- assert(outputType);
38
- return new Case(outputType, branches, otherwise);
39
- }
40
-
41
- evaluate(ctx) {
42
- for (const [test, expression] of this.branches) {
43
- if (test.evaluate(ctx)) {
44
- return expression.evaluate(ctx);
45
- }
46
- }
47
- return this.otherwise.evaluate(ctx);
48
- }
49
-
50
- eachChild(fn) {
51
- for (const [test, expression] of this.branches) {
52
- fn(test);
53
- fn(expression);
54
- }
55
- fn(this.otherwise);
56
- }
57
-
58
- possibleOutputs() {
59
- return []
60
- .concat(...this.branches.map(branch => branch[1].possibleOutputs()))
61
- .concat(this.otherwise.possibleOutputs());
62
- }
63
-
64
- serialize() {
65
- const serialized = ['case'];
66
- this.eachChild(child => {
67
- serialized.push(child.serialize());
68
- });
69
- return serialized;
70
- }
71
- }
72
-
73
- module.exports = Case;
@@ -1,68 +0,0 @@
1
- const assert = require('assert');
2
-
3
- const { checkSubtype, ValueType } = require('../types');
4
-
5
- class Coalesce {
6
- constructor(type, args) {
7
- this.type = type;
8
- this.args = args;
9
- }
10
-
11
- static parse(args, context) {
12
- if (args.length < 2) {
13
- return context.error('Expectected at least one argument.');
14
- }
15
- let outputType = null;
16
- const expectedType = context.expectedType;
17
- if (expectedType && expectedType.kind !== 'value') {
18
- outputType = expectedType;
19
- }
20
- const parsedArgs = [];
21
-
22
- for (const arg of args.slice(1)) {
23
- const parsed = context.parse(arg, 1 + parsedArgs.length, outputType, undefined, {
24
- omitTypeAnnotations: true
25
- });
26
- if (!parsed) return null;
27
- outputType = outputType || parsed.type;
28
- parsedArgs.push(parsed);
29
- }
30
- assert(outputType);
31
-
32
- // Above, we parse arguments without inferred type annotation so that
33
- // they don't produce a runtime error for `null` input, which would
34
- // preempt the desired null-coalescing behavior.
35
- // Thus, if any of our arguments would have needed an annotation, we
36
- // need to wrap the enclosing coalesce expression with it instead.
37
- const needsAnnotation = expectedType && parsedArgs.some(arg => checkSubtype(expectedType, arg.type));
38
-
39
- return needsAnnotation ? new Coalesce(ValueType, parsedArgs) : new Coalesce(outputType, parsedArgs);
40
- }
41
-
42
- evaluate(ctx) {
43
- let result = null;
44
- for (const arg of this.args) {
45
- result = arg.evaluate(ctx);
46
- if (result !== null) break;
47
- }
48
- return result;
49
- }
50
-
51
- eachChild(fn) {
52
- this.args.forEach(fn);
53
- }
54
-
55
- possibleOutputs() {
56
- return [].concat(...this.args.map(arg => arg.possibleOutputs()));
57
- }
58
-
59
- serialize() {
60
- const serialized = ['coalesce'];
61
- this.eachChild(child => {
62
- serialized.push(child.serialize());
63
- });
64
- return serialized;
65
- }
66
- }
67
-
68
- module.exports = Coalesce;
@@ -1,109 +0,0 @@
1
- const assert = require('assert');
2
-
3
- const { ColorType, ValueType, NumberType } = require('../types');
4
- const { Color, validateRGBA } = require('../values');
5
- const RuntimeError = require('../runtime_error');
6
- const { Formatted, FormattedSection } = require('./formatted');
7
-
8
- const types = {
9
- 'to-number': NumberType,
10
- 'to-color': ColorType
11
- };
12
-
13
- /**
14
- * Special form for error-coalescing coercion expressions "to-number",
15
- * "to-color". Since these coercions can fail at runtime, they accept multiple
16
- * arguments, only evaluating one at a time until one succeeds.
17
- *
18
- * @private
19
- */
20
- class Coercion {
21
- constructor(type, args) {
22
- this.type = type;
23
- this.args = args;
24
- }
25
-
26
- static parse(args, context) {
27
- if (args.length < 2) return context.error('Expected at least one argument.');
28
-
29
- const name = args[0];
30
- assert(types[name], name);
31
-
32
- const type = types[name];
33
-
34
- const parsed = [];
35
- for (let i = 1; i < args.length; i++) {
36
- const input = context.parse(args[i], i, ValueType);
37
- if (!input) return null;
38
- parsed.push(input);
39
- }
40
-
41
- return new Coercion(type, parsed);
42
- }
43
-
44
- evaluate(ctx) {
45
- if (this.type.kind === 'color') {
46
- let input;
47
- let error;
48
- for (const arg of this.args) {
49
- input = arg.evaluate(ctx);
50
- error = null;
51
- if (typeof input === 'string') {
52
- const c = ctx.parseColor(input);
53
- if (c) return c;
54
- } else if (Array.isArray(input)) {
55
- if (input.length < 3 || input.length > 4) {
56
- error = `Invalid rbga value ${JSON.stringify(input)}: expected an array containing either three or four numeric values.`;
57
- } else {
58
- error = validateRGBA(input[0], input[1], input[2], input[3]);
59
- }
60
- if (!error) {
61
- return new Color(input[0] / 255, input[1] / 255, input[2] / 255, input[3]);
62
- }
63
- }
64
- }
65
- throw new RuntimeError(
66
- error || `Could not parse color from value '${typeof input === 'string' ? input : JSON.stringify(input)}'`
67
- );
68
- }
69
- if (this.type.kind === 'formatted') {
70
- let input;
71
- for (const arg of this.args) {
72
- input = arg.evaluate(ctx);
73
- if (typeof input === 'string') {
74
- return new Formatted([new FormattedSection(input, null, null)]);
75
- }
76
- }
77
- throw new RuntimeError(
78
- `Could not parse formatted text from value '${typeof input === 'string' ? input : JSON.stringify(input)}'`
79
- );
80
- }
81
- let value = null;
82
- for (const arg of this.args) {
83
- value = arg.evaluate(ctx);
84
- if (value === null) continue;
85
- const num = Number(value);
86
- if (isNaN(num)) continue;
87
- return num;
88
- }
89
- throw new RuntimeError(`Could not convert ${JSON.stringify(value)} to number.`);
90
- }
91
-
92
- eachChild(fn) {
93
- this.args.forEach(fn);
94
- }
95
-
96
- possibleOutputs() {
97
- return [].concat(...this.args.map(arg => arg.possibleOutputs()));
98
- }
99
-
100
- serialize() {
101
- const serialized = [`to-${this.type.kind}`];
102
- this.eachChild(child => {
103
- serialized.push(child.serialize());
104
- });
105
- return serialized;
106
- }
107
- }
108
-
109
- module.exports = Coercion;
@@ -1,102 +0,0 @@
1
- const { StringType, BooleanType, CollatorType } = require('../types');
2
-
3
- class Collator {
4
- constructor(caseSensitive, diacriticSensitive, locale) {
5
- if (caseSensitive) this.sensitivity = diacriticSensitive ? 'variant' : 'case';
6
- else this.sensitivity = diacriticSensitive ? 'accent' : 'base';
7
-
8
- this.locale = locale;
9
- this.collator = new Intl.Collator(this.locale ? this.locale : [], {
10
- sensitivity: this.sensitivity,
11
- usage: 'search'
12
- });
13
- }
14
-
15
- compare(lhs, rhs) {
16
- return this.collator.compare(lhs, rhs);
17
- }
18
-
19
- resolvedLocale() {
20
- // We create a Collator without "usage: search" because we don't want
21
- // the search options encoded in our result (e.g. "en-u-co-search")
22
- return new Intl.Collator(this.locale ? this.locale : []).resolvedOptions().locale;
23
- }
24
- }
25
-
26
- class CollatorExpression {
27
- constructor(caseSensitive, diacriticSensitive, locale) {
28
- this.type = CollatorType;
29
- this.locale = locale;
30
- this.caseSensitive = caseSensitive;
31
- this.diacriticSensitive = diacriticSensitive;
32
- }
33
-
34
- static parse(args, context) {
35
- if (args.length !== 2) return context.error('Expected one argument.');
36
-
37
- const options = args[1];
38
- if (typeof options !== 'object' || Array.isArray(options))
39
- return context.error('Collator options argument must be an object.');
40
-
41
- const caseSensitive = context.parse(
42
- options['case-sensitive'] === undefined ? false : options['case-sensitive'],
43
- 1,
44
- BooleanType
45
- );
46
- if (!caseSensitive) return null;
47
-
48
- const diacriticSensitive = context.parse(
49
- options['diacritic-sensitive'] === undefined ? false : options['diacritic-sensitive'],
50
- 1,
51
- BooleanType
52
- );
53
- if (!diacriticSensitive) return null;
54
-
55
- let locale = null;
56
- if (options['locale']) {
57
- locale = context.parse(options['locale'], 1, StringType);
58
- if (!locale) return null;
59
- }
60
-
61
- return new CollatorExpression(caseSensitive, diacriticSensitive, locale);
62
- }
63
-
64
- evaluate(ctx) {
65
- return new Collator(
66
- this.caseSensitive.evaluate(ctx),
67
- this.diacriticSensitive.evaluate(ctx),
68
- this.locale ? this.locale.evaluate(ctx) : null
69
- );
70
- }
71
-
72
- eachChild(fn) {
73
- fn(this.caseSensitive);
74
- fn(this.diacriticSensitive);
75
- if (this.locale) {
76
- fn(this.locale);
77
- }
78
- }
79
-
80
- possibleOutputs() {
81
- // Technically the set of possible outputs is the combinatoric set of Collators produced
82
- // by all possibleOutputs of locale/caseSensitive/diacriticSensitive
83
- // But for the primary use of Collators in comparison operators, we ignore the Collator's
84
- // possibleOutputs anyway, so we can get away with leaving this undefined for now.
85
- return [undefined];
86
- }
87
-
88
- serialize() {
89
- const options = {};
90
- options['case-sensitive'] = this.caseSensitive.serialize();
91
- options['diacritic-sensitive'] = this.diacriticSensitive.serialize();
92
- if (this.locale) {
93
- options['locale'] = this.locale.serialize();
94
- }
95
- return ['collator', options];
96
- }
97
- }
98
-
99
- module.exports = {
100
- Collator,
101
- CollatorExpression
102
- };
@@ -1,193 +0,0 @@
1
- const { toString, ValueType, BooleanType, CollatorType } = require('../types');
2
- const Assertion = require('./assertion');
3
- const { typeOf } = require('../values');
4
- const RuntimeError = require('../runtime_error');
5
-
6
- function isComparableType(op, type) {
7
- if (op === '==' || op === '!=') {
8
- // equality operator
9
- return (
10
- type.kind === 'boolean' ||
11
- type.kind === 'string' ||
12
- type.kind === 'number' ||
13
- type.kind === 'null' ||
14
- type.kind === 'value'
15
- );
16
- }
17
- // ordering operator
18
- return type.kind === 'string' || type.kind === 'number' || type.kind === 'value';
19
- }
20
-
21
- function eq(ctx, a, b) {
22
- return a === b;
23
- }
24
- function neq(ctx, a, b) {
25
- return a !== b;
26
- }
27
- function lt(ctx, a, b) {
28
- return a < b;
29
- }
30
- function gt(ctx, a, b) {
31
- return a > b;
32
- }
33
- function lteq(ctx, a, b) {
34
- return a <= b;
35
- }
36
- function gteq(ctx, a, b) {
37
- return a >= b;
38
- }
39
-
40
- function eqCollate(ctx, a, b, c) {
41
- return c.compare(a, b) === 0;
42
- }
43
- function neqCollate(ctx, a, b, c) {
44
- return !eqCollate(ctx, a, b, c);
45
- }
46
- function ltCollate(ctx, a, b, c) {
47
- return c.compare(a, b) < 0;
48
- }
49
- function gtCollate(ctx, a, b, c) {
50
- return c.compare(a, b) > 0;
51
- }
52
- function lteqCollate(ctx, a, b, c) {
53
- return c.compare(a, b) <= 0;
54
- }
55
- function gteqCollate(ctx, a, b, c) {
56
- return c.compare(a, b) >= 0;
57
- }
58
-
59
- /**
60
- * Special form for comparison operators, implementing the signatures:
61
- * - (T, T, ?Collator) => boolean
62
- * - (T, value, ?Collator) => boolean
63
- * - (value, T, ?Collator) => boolean
64
- *
65
- * For inequalities, T must be either value, string, or number. For ==/!=, it
66
- * can also be boolean or null.
67
- *
68
- * Equality semantics are equivalent to Javascript's strict equality (===/!==)
69
- * -- i.e., when the arguments' types don't match, == evaluates to false, != to
70
- * true.
71
- *
72
- * When types don't match in an ordering comparison, a runtime error is thrown.
73
- *
74
- * @private
75
- */
76
- function makeComparison(op, compareBasic, compareWithCollator) {
77
- const isOrderComparison = op !== '==' && op !== '!=';
78
-
79
- return class Comparison {
80
- constructor(lhs, rhs, collator) {
81
- this.type = BooleanType;
82
- this.lhs = lhs;
83
- this.rhs = rhs;
84
- this.collator = collator;
85
- this.hasUntypedArgument = lhs.type.kind === 'value' || rhs.type.kind === 'value';
86
- }
87
-
88
- static parse(args, context) {
89
- if (args.length !== 3 && args.length !== 4) return context.error('Expected two or three arguments.');
90
-
91
- const op = args[0];
92
-
93
- let lhs = context.parse(args[1], 1, ValueType);
94
- if (!lhs) return null;
95
- if (!isComparableType(op, lhs.type)) {
96
- return context.concat(1).error(`"${op}" comparisons are not supported for type '${toString(lhs.type)}'.`);
97
- }
98
- let rhs = context.parse(args[2], 2, ValueType);
99
- if (!rhs) return null;
100
- if (!isComparableType(op, rhs.type)) {
101
- return context.concat(2).error(`"${op}" comparisons are not supported for type '${toString(rhs.type)}'.`);
102
- }
103
-
104
- if (lhs.type.kind !== rhs.type.kind && lhs.type.kind !== 'value' && rhs.type.kind !== 'value') {
105
- return context.error(`Cannot compare types '${toString(lhs.type)}' and '${toString(rhs.type)}'.`);
106
- }
107
-
108
- if (isOrderComparison) {
109
- // typing rules specific to less/greater than operators
110
- if (lhs.type.kind === 'value' && rhs.type.kind !== 'value') {
111
- // (value, T)
112
- lhs = new Assertion(rhs.type, [lhs]);
113
- } else if (lhs.type.kind !== 'value' && rhs.type.kind === 'value') {
114
- // (T, value)
115
- rhs = new Assertion(lhs.type, [rhs]);
116
- }
117
- }
118
-
119
- let collator = null;
120
- if (args.length === 4) {
121
- if (
122
- lhs.type.kind !== 'string' &&
123
- rhs.type.kind !== 'string' &&
124
- lhs.type.kind !== 'value' &&
125
- rhs.type.kind !== 'value'
126
- ) {
127
- return context.error('Cannot use collator to compare non-string types.');
128
- }
129
- collator = context.parse(args[3], 3, CollatorType);
130
- if (!collator) return null;
131
- }
132
-
133
- return new Comparison(lhs, rhs, collator);
134
- }
135
-
136
- evaluate(ctx) {
137
- const lhs = this.lhs.evaluate(ctx);
138
- const rhs = this.rhs.evaluate(ctx);
139
-
140
- if (isOrderComparison && this.hasUntypedArgument) {
141
- const lt = typeOf(lhs);
142
- const rt = typeOf(rhs);
143
- // check that type is string or number, and equal
144
- if (lt.kind !== rt.kind || !(lt.kind === 'string' || lt.kind === 'number')) {
145
- throw new RuntimeError(
146
- `Expected arguments for "${op}" to be (string, string) or (number, number), but found (${lt.kind}, ${rt.kind}) instead.`
147
- );
148
- }
149
- }
150
-
151
- if (this.collator && !isOrderComparison && this.hasUntypedArgument) {
152
- const lt = typeOf(lhs);
153
- const rt = typeOf(rhs);
154
- if (lt.kind !== 'string' || rt.kind !== 'string') {
155
- return compareBasic(ctx, lhs, rhs);
156
- }
157
- }
158
-
159
- return this.collator
160
- ? compareWithCollator(ctx, lhs, rhs, this.collator.evaluate(ctx))
161
- : compareBasic(ctx, lhs, rhs);
162
- }
163
-
164
- eachChild(fn) {
165
- fn(this.lhs);
166
- fn(this.rhs);
167
- if (this.collator) {
168
- fn(this.collator);
169
- }
170
- }
171
-
172
- possibleOutputs() {
173
- return [true, false];
174
- }
175
-
176
- serialize() {
177
- const serialized = [op];
178
- this.eachChild(child => {
179
- serialized.push(child.serialize());
180
- });
181
- return serialized;
182
- }
183
- };
184
- }
185
-
186
- module.exports = {
187
- Equals: makeComparison('==', eq, eqCollate),
188
- NotEquals: makeComparison('!=', neq, neqCollate),
189
- LessThan: makeComparison('<', lt, ltCollate),
190
- GreaterThan: makeComparison('>', gt, gtCollate),
191
- LessThanOrEqual: makeComparison('<=', lteq, lteqCollate),
192
- GreaterThanOrEqual: makeComparison('>=', gteq, gteqCollate)
193
- };