From 4ce512435098f993858c3a9b640f5ff6f7fbdb21 Mon Sep 17 00:00:00 2001 From: maxonfjvipon Date: Tue, 21 May 2024 21:27:52 +0300 Subject: [PATCH] Refactoring --- .../src/objects/org/eolang/float$div.js | 3 +- .../src/objects/org/eolang/float$gt.js | 3 +- .../src/objects/org/eolang/float$plus.js | 3 +- .../src/objects/org/eolang/float$times.js | 3 +- .../src/objects/org/eolang/int$div.js | 3 +- .../src/objects/org/eolang/int$gt.js | 3 +- .../src/objects/org/eolang/int$plus.js | 3 +- .../src/objects/org/eolang/int$times.js | 3 +- .../src/objects/org/eolang/io/stdout.js | 3 +- .../src/objects/org/eolang/string$length.js | 2 +- eo2js-runtime/src/runtime/data.js | 8 +- eo2js-runtime/src/runtime/error/ErError.js | 4 +- eo2js-runtime/src/runtime/trapped.js | 3 - eo2js-runtime/src/runtime/validated.js | 2 +- .../test/objects/org/eolang/error.test.js | 2 +- .../test/objects/org/eolang/float$div.test.js | 12 +-- .../test/objects/org/eolang/float$gt.test.js | 12 +-- .../objects/org/eolang/float$plus.test.js | 7 +- .../objects/org/eolang/float$times.test.js | 7 +- .../test/objects/org/eolang/int$div.test.js | 7 +- .../test/objects/org/eolang/int$gt.test.js | 12 +-- .../test/objects/org/eolang/int$plus.test.js | 7 +- .../test/objects/org/eolang/int$times.test.js | 7 +- .../test/objects/org/eolang/io/stdout.test.js | 12 +-- .../test/objects/org/eolang/try.test.js | 8 +- eo2js-runtime/test/runtime/data.test.js | 10 +- eo2js/src/commands/link.js | 3 +- eo2js/src/commands/transpile.js | 98 ++++++++++++------- eo2js/src/eo2js.js | 2 - eo2js/src/resources/js/__main__.js | 4 +- eo2js/test/it/it.test.js | 10 +- 31 files changed, 116 insertions(+), 150 deletions(-) diff --git a/eo2js-runtime/src/objects/org/eolang/float$div.js b/eo2js-runtime/src/objects/org/eolang/float$div.js index d52caae..5f33fe0 100644 --- a/eo2js-runtime/src/objects/org/eolang/float$div.js +++ b/eo2js-runtime/src/objects/org/eolang/float$div.js @@ -1,7 +1,6 @@ const dataized = require('../../../runtime/dataized') const object = require('../../../runtime/object') -const data = require('../../../runtime/data') -const {FLOAT} = require('../../../runtime/data') +const {data, FLOAT} = require('../../../runtime/data') const {LAMBDA, RHO} = require('../../../runtime/attribute/specials') const at_void = require('../../../runtime/attribute/at-void') diff --git a/eo2js-runtime/src/objects/org/eolang/float$gt.js b/eo2js-runtime/src/objects/org/eolang/float$gt.js index 014cfb3..71fb694 100644 --- a/eo2js-runtime/src/objects/org/eolang/float$gt.js +++ b/eo2js-runtime/src/objects/org/eolang/float$gt.js @@ -1,7 +1,6 @@ const dataized = require('../../../runtime/dataized') const object = require('../../../runtime/object') -const data = require('../../../runtime/data') -const {FLOAT} = require('../../../runtime/data') +const {data, FLOAT} = require('../../../runtime/data') const {LAMBDA, RHO} = require('../../../runtime/attribute/specials') const at_void = require('../../../runtime/attribute/at-void') diff --git a/eo2js-runtime/src/objects/org/eolang/float$plus.js b/eo2js-runtime/src/objects/org/eolang/float$plus.js index 1f625ad..8c18d12 100644 --- a/eo2js-runtime/src/objects/org/eolang/float$plus.js +++ b/eo2js-runtime/src/objects/org/eolang/float$plus.js @@ -1,7 +1,6 @@ const dataized = require('../../../runtime/dataized') const object = require('../../../runtime/object') -const data = require('../../../runtime/data') -const {FLOAT} = require('../../../runtime/data') +const {data, FLOAT} = require('../../../runtime/data') const {LAMBDA, RHO} = require('../../../runtime/attribute/specials') const at_void = require('../../../runtime/attribute/at-void') diff --git a/eo2js-runtime/src/objects/org/eolang/float$times.js b/eo2js-runtime/src/objects/org/eolang/float$times.js index c29bbb1..a722432 100644 --- a/eo2js-runtime/src/objects/org/eolang/float$times.js +++ b/eo2js-runtime/src/objects/org/eolang/float$times.js @@ -1,7 +1,6 @@ const dataized = require('../../../runtime/dataized') const object = require('../../../runtime/object') -const data = require('../../../runtime/data') -const {FLOAT} = require('../../../runtime/data') +const {data, FLOAT} = require('../../../runtime/data') const {LAMBDA, RHO} = require('../../../runtime/attribute/specials') const at_void = require('../../../runtime/attribute/at-void') diff --git a/eo2js-runtime/src/objects/org/eolang/int$div.js b/eo2js-runtime/src/objects/org/eolang/int$div.js index 8b480b6..17238a7 100644 --- a/eo2js-runtime/src/objects/org/eolang/int$div.js +++ b/eo2js-runtime/src/objects/org/eolang/int$div.js @@ -1,7 +1,6 @@ const dataized = require('../../../runtime/dataized') const object = require('../../../runtime/object') -const data = require('../../../runtime/data') -const {INT} = require('../../../runtime/data') +const {data, INT} = require('../../../runtime/data') const {LAMBDA, RHO} = require('../../../runtime/attribute/specials') const at_void = require('../../../runtime/attribute/at-void') diff --git a/eo2js-runtime/src/objects/org/eolang/int$gt.js b/eo2js-runtime/src/objects/org/eolang/int$gt.js index 93f0515..60f0662 100644 --- a/eo2js-runtime/src/objects/org/eolang/int$gt.js +++ b/eo2js-runtime/src/objects/org/eolang/int$gt.js @@ -1,7 +1,6 @@ const dataized = require('../../../runtime/dataized') const object = require('../../../runtime/object') -const data = require('../../../runtime/data') -const {INT} = require('../../../runtime/data') +const {data, INT} = require('../../../runtime/data') const {LAMBDA, RHO} = require('../../../runtime/attribute/specials') const at_void = require('../../../runtime/attribute/at-void') diff --git a/eo2js-runtime/src/objects/org/eolang/int$plus.js b/eo2js-runtime/src/objects/org/eolang/int$plus.js index d8d8019..3cf485a 100644 --- a/eo2js-runtime/src/objects/org/eolang/int$plus.js +++ b/eo2js-runtime/src/objects/org/eolang/int$plus.js @@ -1,7 +1,6 @@ const dataized = require('../../../runtime/dataized') const object = require('../../../runtime/object') -const data = require('../../../runtime/data') -const {INT} = require('../../../runtime/data') +const {data, INT} = require('../../../runtime/data') const {LAMBDA, RHO} = require('../../../runtime/attribute/specials') const at_void = require('../../../runtime/attribute/at-void') diff --git a/eo2js-runtime/src/objects/org/eolang/int$times.js b/eo2js-runtime/src/objects/org/eolang/int$times.js index d9b5433..a335d50 100644 --- a/eo2js-runtime/src/objects/org/eolang/int$times.js +++ b/eo2js-runtime/src/objects/org/eolang/int$times.js @@ -1,7 +1,6 @@ const dataized = require('../../../runtime/dataized') const object = require('../../../runtime/object') -const data = require('../../../runtime/data') -const {INT} = require('../../../runtime/data') +const {data, INT} = require('../../../runtime/data') const {LAMBDA, RHO} = require('../../../runtime/attribute/specials') const at_void = require('../../../runtime/attribute/at-void') diff --git a/eo2js-runtime/src/objects/org/eolang/io/stdout.js b/eo2js-runtime/src/objects/org/eolang/io/stdout.js index 2ad33a7..43aa124 100644 --- a/eo2js-runtime/src/objects/org/eolang/io/stdout.js +++ b/eo2js-runtime/src/objects/org/eolang/io/stdout.js @@ -1,7 +1,6 @@ const object = require('../../../../runtime/object.js') const dataized = require('../../../../runtime/dataized') -const data = require('../../../../runtime/data') -const {STRING} = require('../../../../runtime/data') +const {data, STRING} = require('../../../../runtime/data') const {LAMBDA} = require('../../../../runtime/attribute/specials') const at_void = require('../../../../runtime/attribute/at-void'); diff --git a/eo2js-runtime/src/objects/org/eolang/string$length.js b/eo2js-runtime/src/objects/org/eolang/string$length.js index 3f05078..c35aaee 100644 --- a/eo2js-runtime/src/objects/org/eolang/string$length.js +++ b/eo2js-runtime/src/objects/org/eolang/string$length.js @@ -1,6 +1,6 @@ const object = require('../../../runtime/object') const {LAMBDA, RHO} = require('../../../runtime/attribute/specials'); -const data = require('../../../runtime/data'); +const {data} = require('../../../runtime/data'); const dataized = require('../../../runtime/dataized'); /** diff --git a/eo2js-runtime/src/runtime/data.js b/eo2js-runtime/src/runtime/data.js index 0956fab..ece357b 100644 --- a/eo2js-runtime/src/runtime/data.js +++ b/eo2js-runtime/src/runtime/data.js @@ -39,12 +39,14 @@ const data = { }) } return object - }, + } +} + +module.exports = { + data, INT: 'int', STRING: 'string', FLOAT: 'float', BOOL: 'bool', BYTES: 'bytes' } - -module.exports = data diff --git a/eo2js-runtime/src/runtime/error/ErError.js b/eo2js-runtime/src/runtime/error/ErError.js index eb51082..65ae713 100644 --- a/eo2js-runtime/src/runtime/error/ErError.js +++ b/eo2js-runtime/src/runtime/error/ErError.js @@ -1,6 +1,6 @@ const ErAbstract = require('./ErAbstract'); const dataized = require('../dataized'); -const data = require('../data'); +const {STRING} = require('../data'); /** * This Error is thrown by the {@see error} object only. @@ -22,7 +22,7 @@ class ErError extends ErAbstract { * @return {string} */ static safeMessage(enclosure) { - return dataized(enclosure, data.STRING) + return dataized(enclosure, STRING) } } diff --git a/eo2js-runtime/src/runtime/trapped.js b/eo2js-runtime/src/runtime/trapped.js index 90c3513..d0f1718 100644 --- a/eo2js-runtime/src/runtime/trapped.js +++ b/eo2js-runtime/src/runtime/trapped.js @@ -9,9 +9,6 @@ const trapped = function(callback) { { get: function(target, property, _) { return target()[property] - }, - apply: function(target, thisArg, arguments) { - return target().call(thisArg, ...arguments) } } ) diff --git a/eo2js-runtime/src/runtime/validated.js b/eo2js-runtime/src/runtime/validated.js index 8e11a2a..9201f2c 100644 --- a/eo2js-runtime/src/runtime/validated.js +++ b/eo2js-runtime/src/runtime/validated.js @@ -1,6 +1,6 @@ const ErFailure = require('./error/ErFailure'); const ErError = require('./error/ErError'); -const data = require('./data'); +const {data} = require('./data'); /** * Validate given callback. diff --git a/eo2js-runtime/test/objects/org/eolang/error.test.js b/eo2js-runtime/test/objects/org/eolang/error.test.js index ae4a270..70f0385 100644 --- a/eo2js-runtime/test/objects/org/eolang/error.test.js +++ b/eo2js-runtime/test/objects/org/eolang/error.test.js @@ -1,6 +1,6 @@ const assert = require('assert'); const error = require('../../../../temp/objects/org/eolang/error'); -const data = require('../../../../temp/runtime/data'); +const {data} = require('../../../../temp/runtime/data'); const dataized = require('../../../../temp/runtime/dataized'); const ErError = require('../../../../temp/runtime/error/ErError'); diff --git a/eo2js-runtime/test/objects/org/eolang/float$div.test.js b/eo2js-runtime/test/objects/org/eolang/float$div.test.js index 5348612..1cc70b2 100644 --- a/eo2js-runtime/test/objects/org/eolang/float$div.test.js +++ b/eo2js-runtime/test/objects/org/eolang/float$div.test.js @@ -1,6 +1,6 @@ const assert = require('assert'); const float$div = require('../../../../temp/objects/org/eolang/float$div'); -const data = require('../../../../temp/runtime/data'); +const {data, FLOAT} = require('../../../../temp/runtime/data'); const dataized = require('../../../../temp/runtime/dataized'); const {RHO} = require('../../../../temp/runtime/attribute/specials'); const at_rho = require('../../../../temp/runtime/attribute/at-rho'); @@ -10,10 +10,7 @@ describe('float$div', function() { const div = float$div() div.attrs[RHO] = at_rho(data.toObject(13.2)) assert.equal( - dataized( - div.with({'x': data.toObject(5.7)}), - data.FLOAT - ), + dataized(div.with({'x': data.toObject(5.7)}), FLOAT), 13.2 / 5.7 ) }) @@ -21,10 +18,7 @@ describe('float$div', function() { const div = float$div() div.attrs[RHO] = at_rho(data.toObject(13.2)) assert.doesNotThrow( - () => dataized( - div.with({'x': data.toObject(0.0)}), - data.FLOAT - ) + () => dataized(div.with({'x': data.toObject(0.0)}), FLOAT) ) }); }) diff --git a/eo2js-runtime/test/objects/org/eolang/float$gt.test.js b/eo2js-runtime/test/objects/org/eolang/float$gt.test.js index db6e4f1..7036951 100644 --- a/eo2js-runtime/test/objects/org/eolang/float$gt.test.js +++ b/eo2js-runtime/test/objects/org/eolang/float$gt.test.js @@ -1,6 +1,6 @@ const assert = require('assert'); const float$gt = require('../../../../temp/objects/org/eolang/float$gt'); -const data = require('../../../../temp/runtime/data'); +const {data, BOOL} = require('../../../../temp/runtime/data'); const dataized = require('../../../../temp/runtime/dataized'); const {RHO} = require('../../../../temp/runtime/attribute/specials'); const at_rho = require('../../../../temp/runtime/attribute/at-rho'); @@ -10,10 +10,7 @@ describe('float$gt', function() { const gt = float$gt() gt.attrs[RHO] = at_rho(data.toObject(7.2)) assert.equal( - dataized( - gt.with({'x': data.toObject(3.1)}), - data.BOOL - ), + dataized(gt.with({'x': data.toObject(3.1)}), BOOL), true ) }) @@ -21,10 +18,7 @@ describe('float$gt', function() { const gt = float$gt() gt.attrs[RHO] = at_rho(data.toObject(10.4)) assert.equal( - dataized( - gt.with({'x': data.toObject(20.1)}), - data.BOOL - ), + dataized(gt.with({'x': data.toObject(20.1)}), BOOL), false ) }) diff --git a/eo2js-runtime/test/objects/org/eolang/float$plus.test.js b/eo2js-runtime/test/objects/org/eolang/float$plus.test.js index b823048..a460508 100644 --- a/eo2js-runtime/test/objects/org/eolang/float$plus.test.js +++ b/eo2js-runtime/test/objects/org/eolang/float$plus.test.js @@ -1,6 +1,6 @@ const assert = require('assert'); const float$plus = require('../../../../temp/objects/org/eolang/float$plus'); -const data = require('../../../../temp/runtime/data'); +const {data, FLOAT} = require('../../../../temp/runtime/data'); const dataized = require('../../../../temp/runtime/dataized'); const {RHO} = require('../../../../temp/runtime/attribute/specials'); const at_rho = require('../../../../temp/runtime/attribute/at-rho'); @@ -10,10 +10,7 @@ describe('int$plus', function() { const plus = float$plus() plus.attrs[RHO] = at_rho(data.toObject(7.1)) assert.equal( - dataized( - plus.with({'x': data.toObject(3.4)}), - data.FLOAT - ), + dataized(plus.with({'x': data.toObject(3.4)}), FLOAT), 10.5 ) }) diff --git a/eo2js-runtime/test/objects/org/eolang/float$times.test.js b/eo2js-runtime/test/objects/org/eolang/float$times.test.js index 7268348..b9b36fe 100644 --- a/eo2js-runtime/test/objects/org/eolang/float$times.test.js +++ b/eo2js-runtime/test/objects/org/eolang/float$times.test.js @@ -1,5 +1,5 @@ const assert = require('assert'); -const data = require('../../../../temp/runtime/data'); +const {data, FLOAT} = require('../../../../temp/runtime/data'); const dataized = require('../../../../temp/runtime/dataized'); const float$times = require('../../../../temp/objects/org/eolang/float$times'); const {RHO} = require('../../../../temp/runtime/attribute/specials'); @@ -10,10 +10,7 @@ describe('float$times', function() { const times = float$times() times.attrs[RHO] = at_rho(data.toObject(5.5)) assert.equal( - dataized( - times.with({'x': data.toObject(12.1)}), - data.FLOAT - ), + dataized(times.with({'x': data.toObject(12.1)}), FLOAT), 5.5 * 12.1 ) }) diff --git a/eo2js-runtime/test/objects/org/eolang/int$div.test.js b/eo2js-runtime/test/objects/org/eolang/int$div.test.js index 183d85d..da3b555 100644 --- a/eo2js-runtime/test/objects/org/eolang/int$div.test.js +++ b/eo2js-runtime/test/objects/org/eolang/int$div.test.js @@ -1,6 +1,6 @@ const assert = require('assert'); const int$div = require('../../../../temp/objects/org/eolang/int$div'); -const data = require('../../../../temp/runtime/data'); +const {data, INT} = require('../../../../temp/runtime/data'); const dataized = require('../../../../temp/runtime/dataized'); const {RHO} = require('../../../../temp/runtime/attribute/specials'); const at_rho = require('../../../../temp/runtime/attribute/at-rho'); @@ -10,10 +10,7 @@ describe('int$div', function() { const div = int$div() div.attrs[RHO] = at_rho(data.toObject(42)) assert.equal( - dataized( - div.with({'x': data.toObject(6)}), - data.INT - ), + dataized(div.with({'x': data.toObject(6)}), INT), 7 ) }) diff --git a/eo2js-runtime/test/objects/org/eolang/int$gt.test.js b/eo2js-runtime/test/objects/org/eolang/int$gt.test.js index 80a373e..8aa2f3e 100644 --- a/eo2js-runtime/test/objects/org/eolang/int$gt.test.js +++ b/eo2js-runtime/test/objects/org/eolang/int$gt.test.js @@ -1,6 +1,6 @@ const assert = require('assert'); const int$gt = require('../../../../temp/objects/org/eolang/int$gt'); -const data = require('../../../../temp/runtime/data'); +const {data, BOOL} = require('../../../../temp/runtime/data'); const dataized = require('../../../../temp/runtime/dataized'); const {RHO} = require('../../../../temp/runtime/attribute/specials'); const at_rho = require('../../../../temp/runtime/attribute/at-rho'); @@ -10,10 +10,7 @@ describe('int$gt', function() { const gt = int$gt() gt.attrs[RHO] = at_rho(data.toObject(7)) assert.equal( - dataized( - gt.with({'x': data.toObject(3)}), - data.BOOL - ), + dataized(gt.with({'x': data.toObject(3)}), BOOL), true ) }) @@ -21,10 +18,7 @@ describe('int$gt', function() { const gt = int$gt() gt.attrs[RHO] = at_rho(data.toObject(10)) assert.equal( - dataized( - gt.with({'x': data.toObject(20)}), - data.BOOL - ), + dataized(gt.with({'x': data.toObject(20)}), BOOL), false ) }) diff --git a/eo2js-runtime/test/objects/org/eolang/int$plus.test.js b/eo2js-runtime/test/objects/org/eolang/int$plus.test.js index 8650842..38825c5 100644 --- a/eo2js-runtime/test/objects/org/eolang/int$plus.test.js +++ b/eo2js-runtime/test/objects/org/eolang/int$plus.test.js @@ -1,6 +1,6 @@ const assert = require('assert'); const int$plus = require('../../../../temp/objects/org/eolang/int$plus'); -const data = require('../../../../temp/runtime/data'); +const {data, INT} = require('../../../../temp/runtime/data'); const dataized = require('../../../../temp/runtime/dataized'); const {RHO} = require('../../../../temp/runtime/attribute/specials'); const at_rho = require('../../../../temp/runtime/attribute/at-rho'); @@ -10,10 +10,7 @@ describe('int$plus', function() { const plus = int$plus() plus.attrs[RHO] = at_rho(data.toObject(7)) assert.equal( - dataized( - plus.with({'x': data.toObject(3)}), - data.INT - ), + dataized(plus.with({'x': data.toObject(3)}), INT), 10 ) }) diff --git a/eo2js-runtime/test/objects/org/eolang/int$times.test.js b/eo2js-runtime/test/objects/org/eolang/int$times.test.js index ffe96c1..9ab151f 100644 --- a/eo2js-runtime/test/objects/org/eolang/int$times.test.js +++ b/eo2js-runtime/test/objects/org/eolang/int$times.test.js @@ -1,6 +1,6 @@ const assert = require('assert'); const int$times = require('../../../../temp/objects/org/eolang/int$times'); -const data = require('../../../../temp/runtime/data'); +const {data, INT} = require('../../../../temp/runtime/data'); const dataized = require('../../../../temp/runtime/dataized'); const {RHO} = require('../../../../temp/runtime/attribute/specials'); const at_rho = require('../../../../temp/runtime/attribute/at-rho'); @@ -10,10 +10,7 @@ describe('int$times', function() { const times = int$times() times.attrs[RHO] = at_rho(data.toObject(3)) assert.equal( - dataized( - times.with({'x': data.toObject(4)}), - data.INT - ), + dataized(times.with({'x': data.toObject(4)}), INT), 12 ) }) diff --git a/eo2js-runtime/test/objects/org/eolang/io/stdout.test.js b/eo2js-runtime/test/objects/org/eolang/io/stdout.test.js index 3ca3fb0..522b0c3 100644 --- a/eo2js-runtime/test/objects/org/eolang/io/stdout.test.js +++ b/eo2js-runtime/test/objects/org/eolang/io/stdout.test.js @@ -1,24 +1,18 @@ const assert = require('assert'); const stdout = require('../../../../../temp/objects/org/eolang/io/stdout'); -const data = require('../../../../../temp/runtime/data'); +const {data, BOOL} = require('../../../../../temp/runtime/data'); const dataized = require('../../../../../temp/runtime/dataized'); describe('stdout', function() { it('should be dataized to true', function() { assert.ok( - dataized( - stdout().with({'text': data.toObject('Hello')}), - data.BOOL - ) + dataized(stdout().with({'text': data.toObject('Hello')}), BOOL) ) }) it('should log to output', function() { const logs = [] const out = {log: (...args) => logs.push(...args)} - dataized( - stdout(out).with({'text': data.toObject('Hello, world!')}), - data.BOOL - ) + dataized(stdout(out).with({'text': data.toObject('Hello, world!')}), BOOL) assert.ok(logs.includes('Hello, world!')) }); }) diff --git a/eo2js-runtime/test/objects/org/eolang/try.test.js b/eo2js-runtime/test/objects/org/eolang/try.test.js index 09197f4..df92c08 100644 --- a/eo2js-runtime/test/objects/org/eolang/try.test.js +++ b/eo2js-runtime/test/objects/org/eolang/try.test.js @@ -1,6 +1,6 @@ const assert = require('assert'); const tr = require('../../../../temp/objects/org/eolang/try'); -const data = require('../../../../temp/runtime/data'); +const {data, STRING, BOOL} = require('../../../../temp/runtime/data'); const object = require('../../../../temp/runtime/object'); const dataized = require('../../../../temp/runtime/dataized'); const attr = require('../../../../temp/runtime/attribute/attr'); @@ -77,13 +77,13 @@ const tryObj = function(main, final) { describe('try', function() { it(`should catch error via ${PHI}`, function() { - assert.equal(dataized(tryObj(broken(PHI)), data.STRING), message) + assert.equal(dataized(tryObj(broken(PHI)), STRING), message) }) it(`should catch error via ${LAMBDA}`, function() { - assert.equal(dataized(tryObj(broken(LAMBDA)), data.STRING), message) + assert.equal(dataized(tryObj(broken(LAMBDA)), STRING), message) }) it('should be dataized to body', function() { - assert.equal(dataized(tryObj(main()), data.BOOL), true) + assert.equal(dataized(tryObj(main()), BOOL), true) }) it('should dataize "finally" attribute', function() { const logs = [] diff --git a/eo2js-runtime/test/runtime/data.test.js b/eo2js-runtime/test/runtime/data.test.js index 878ce17..25ff940 100644 --- a/eo2js-runtime/test/runtime/data.test.js +++ b/eo2js-runtime/test/runtime/data.test.js @@ -1,5 +1,5 @@ const assert = require('assert'); -const data = require('../../temp/runtime/data') +const {data, INT, STRING, FLOAT, BOOL} = require('../../temp/runtime/data') const dataized = require('../../temp/runtime/dataized'); const isObject = function(obj) { @@ -17,7 +17,7 @@ describe('data', function() { assert.ok(isObject(data.toObject(5))) }) it('should be dataized as int', function() { - assert.equal(dataized(data.toObject(42), data.INT), 42) + assert.equal(dataized(data.toObject(42), INT), 42) }) }) describe('to string', function() { @@ -25,7 +25,7 @@ describe('data', function() { assert.ok(isObject(data.toObject('Hello'))) }) it('should be dataized as string', function() { - assert.equal(dataized(data.toObject('Hello'), data.STRING), 'Hello') + assert.equal(dataized(data.toObject('Hello'), STRING), 'Hello') }) }) describe('to float', function() { @@ -33,7 +33,7 @@ describe('data', function() { assert.ok(isObject(data.toObject(42.7))) }) it('should be dataized as float', function() { - assert.equal(dataized(data.toObject(13.2), data.FLOAT), 13.2) + assert.equal(dataized(data.toObject(13.2), FLOAT), 13.2) }) }) describe('to bool', function() { @@ -41,7 +41,7 @@ describe('data', function() { assert.ok(isObject(data.toObject(true))) }) it('should be dataized as bool', function() { - assert.equal(dataized(data.toObject(false), data.BOOL), false) + assert.equal(dataized(data.toObject(false), BOOL), false) }) }) describe('to bytes', function() { diff --git a/eo2js/src/commands/link.js b/eo2js/src/commands/link.js index e918ad7..6faf153 100644 --- a/eo2js/src/commands/link.js +++ b/eo2js/src/commands/link.js @@ -11,8 +11,7 @@ const main = '__main__.js' /** * Data to insert to package.json file. - * If path to local dependency is present - eo2js-runtime dependency won't be added to - * package.json file. + * If path to local dependency is present - eo2js-runtime dependency won't be added. * @param {String} [runtime] - path to local eo-runtime dependency * @return {{author: string, name: string, version: string}} */ diff --git a/eo2js/src/commands/transpile.js b/eo2js/src/commands/transpile.js index b5252fd..d2f0153 100644 --- a/eo2js/src/commands/transpile.js +++ b/eo2js/src/commands/transpile.js @@ -4,6 +4,18 @@ const fs = require('fs'); const saxon = require('saxon-js') const {XMLParser} = require('fast-xml-parser'); +/** + * Verified key. + * @type {string} + */ +const verified = 'verified' + +/** + * Result directory for transpiled XMIRs. + * @type {string} + */ +const dir = '8-transpile' + /** * Replace dots in given string with {@code path.sep} * @param {String} str - String @@ -74,6 +86,49 @@ const packageMeta = function(xmir) { return meta } +/** + * Transform XMIR from given tojo and save. + * @param {Object} tojo - Tojo. + * @param {{target: String, project: String}} options - Program options + * @param {Array.} transformations - List of transformations to apply to XMIR + * @param {any} parser - XML parser + */ +const transform = function(tojo, options, transformations, parser) { + const text = fs.readFileSync(tojo[verified]).toString() + let xml = parser.parse(text) + const pckg = packageMeta(xml) + const transpiled = path.resolve( + options.target, + dir, + `${pathFromName(xml['program']['@_name'], pckg)}.xmir` + ) + makeDirIfNotExist(transpiled.substring(0, transpiled.lastIndexOf(path.sep))) + fs.writeFileSync(transpiled, text) + xml = text + transformations.forEach((transformation) => { + xml = saxon.transform({ + stylesheetFileName: transformation, + sourceText: xml, + destination: 'serialized' + }).principalResult + }) + fs.writeFileSync(transpiled, xml) + xml = parser.parse(xml) + let objects = xml.program.objects.object + if (!Array.isArray(objects)) { + objects = [objects] + } + const filtered = objects.filter((obj) => !!obj && obj.hasOwnProperty('javascript') && !obj.hasOwnProperty('@_atom')) + const count = hasMeta(xml, 'tests') ? 0 : 1 + if (filtered.length > count) { + const first = filtered[0] + const dest = path.resolve(options.project, `${pathFromName(first['@_js-name'], pckg)}.js`) + makeDirIfNotExist(dest.substring(0, dest.lastIndexOf(path.sep))) + fs.writeFileSync(dest, first['javascript']) + filtered.slice(1).forEach((obj) => fs.appendFileSync(dest, `\n${obj['javascript']}`)) + } +} + /** * Transpile XMIR to JavaScript. * @param {{foreign: String, project: String, resources: String}} options - Transpile command options @@ -91,47 +146,16 @@ const transpile = function(options) { 'objects', 'package', 'tests', 'attrs', 'data', 'to-js' ].map((name) => path.resolve(options['resources'], `json/${name}.sef.json`)) const parser = new XMLParser({ignoreAttributes: false}) - const verified = 'verified' - const dir = '8-transpile' const project = path.resolve(options['target'], options['project']) fs.mkdirSync(project, {recursive: true}) JSON.parse(fs.readFileSync(foreign).toString()) .filter((tojo) => tojo.hasOwnProperty(verified)) - .forEach((tojo) => { - const text = fs.readFileSync(tojo[verified]).toString() - let xml = parser.parse(text) - const pckg = packageMeta(xml) - const transpiled = path.resolve( - options['target'], - dir, - `${pathFromName(xml['program']['@_name'], pckg)}.xmir` - ) - makeDirIfNotExist(transpiled.substring(0, transpiled.lastIndexOf(path.sep))) - fs.writeFileSync(transpiled, text) - xml = text - transformations.forEach((transformation) => { - xml = saxon.transform({ - stylesheetFileName: transformation, - sourceText: xml, - destination: 'serialized' - }).principalResult - }) - fs.writeFileSync(transpiled, xml) - xml = parser.parse(xml) - let objects = xml.program.objects.object - if (!Array.isArray(objects)) { - objects = [objects] - } - const filtered = objects.filter((obj) => !!obj && obj.hasOwnProperty('javascript') && !obj.hasOwnProperty('@_atom')) - const count = hasMeta(xml, 'tests') ? 0 : 1 - if (filtered.length > count) { - const first = filtered[0] - const dest = path.resolve(project, `${pathFromName(first['@_js-name'], pckg)}.js`) - makeDirIfNotExist(dest.substring(0, dest.lastIndexOf(path.sep))) - fs.writeFileSync(dest, first['javascript']) - filtered.slice(1).forEach((obj) => fs.appendFileSync(dest, `\n${obj['javascript']}`)) - } - }) + .forEach((tojo) => transform( + tojo, + {target: options['target'], project}, + transformations, + parser + )) } module.exports = transpile diff --git a/eo2js/src/eo2js.js b/eo2js/src/eo2js.js index 9a03c6e..1c7d36e 100755 --- a/eo2js/src/eo2js.js +++ b/eo2js/src/eo2js.js @@ -25,8 +25,6 @@ program // .option('--latest', 'Use the latest parser version from Maven Central') // .option('-b, --batch', 'Run in batch mode, suppress interactive messages') // .option('--no-color', 'Disable colorization of console messages') - // .option('--track-optimization-steps', 'Save intermediate XMIR files') - // .option('-c, --clean', 'Delete ./.eoc directory') .option('--verbose', 'Print debug messages and full output of child processes'); /** diff --git a/eo2js/src/resources/js/__main__.js b/eo2js/src/resources/js/__main__.js index 195eabf..f78dc48 100644 --- a/eo2js/src/resources/js/__main__.js +++ b/eo2js/src/resources/js/__main__.js @@ -1,11 +1,11 @@ const phi = require('eo2js-runtime/src/runtime/phi') const dataized = require('eo2js-runtime/src/runtime/dataized') const bytesOf = require('eo2js-runtime/src/runtime/bytes-of') -const data = require('eo2js-runtime/src/runtime/data') +const {data} = require('eo2js-runtime/src/runtime/data') const ErAbstract = require('eo2js-runtime/src/runtime/error/ErAbstract') /** - * Run application. + * Application entry point. */ const main = function() { try { diff --git a/eo2js/test/it/it.test.js b/eo2js/test/it/it.test.js index d6dbc27..9ec5bd0 100644 --- a/eo2js/test/it/it.test.js +++ b/eo2js/test/it/it.test.js @@ -13,8 +13,6 @@ describe('integration test', function() { this.timeout(100000) before('recompile stylesheets', function() { compileStylesheets() - }) - beforeEach('clear home', function() { fs.rmSync(home, {recursive: true, force: true}) fs.mkdirSync(project, {recursive: true}) fs.cpSync( @@ -22,21 +20,17 @@ describe('integration test', function() { path.resolve(home, 'src/eo'), {recursive: true} ) - }) - it('should execute simple integration test', function(done) { mvnw( ['register', 'assemble', 'verify'], {home, sources: 'src/eo', target: 'target'} ) + }) + it('should execute simple integration test', function(done) { const log = runSync(['test', '-t', target, '-p project -d', runtime]) assert.ok(log.includes('test "story_is_not_empty" should work')) done() }) it('should dataize simple program', function(done) { - mvnw( - ['register', 'assemble', 'verify'], - {home, sources: 'src/eo', target: 'target'} - ) const log = runSync(['dataize program -t', target, '-p project -d', runtime]) assert.ok(log.includes('Hello, Jeff')) assert.ok(log.includes('true'))