Skip to content
This repository has been archived by the owner on Sep 7, 2023. It is now read-only.

Commit

Permalink
(fix) Make the interface with the engine more uniform between JS targets
Browse files Browse the repository at this point in the history
Signed-off-by: Jerome Simeon <jeromesimeon@me.com>
  • Loading branch information
jeromesimeon committed Mar 24, 2019
1 parent 58e2b39 commit ccca56d
Show file tree
Hide file tree
Showing 21 changed files with 3,775 additions and 3,723 deletions.
22 changes: 12 additions & 10 deletions backends/javascript/ergo-runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -756,17 +756,19 @@ function dateTimeEndOf(part, date) {
return date.endOf(part);
}

/** Target-specific support */

/* Cicero Error handling */
function ciceroError(result) {
var failure = toRight(result);
var message = "Unknown Ergo Logic Error (Please file a GitHub issue)";
if (either(cast(["org.accordproject.ergo.stdlib.ErgoErrorResponse"],failure))) {
message = unbrand(toLeft(cast(["org.accordproject.ergo.stdlib.ErgoErrorResponse"],failure))).message;
/* Unwrapping errors on output */
function unwrapError(result) {
if (result.hasOwnProperty('left')) {
return toLeft(result);
} else {
message = JSON.stringify(toRight(cast(["org.accordproject.ergo.stdlib.ErgoErrorResponse"],failure)));
var failure = toRight(result);
var message = "Unknown Ergo Logic Error (Please file a GitHub issue)";
if (either(cast(["org.accordproject.ergo.stdlib.ErgoErrorResponse"],failure))) {
message = unbrand(toLeft(cast(["org.accordproject.ergo.stdlib.ErgoErrorResponse"],failure))).message;
} else {
message = JSON.stringify(toRight(cast(["org.accordproject.ergo.stdlib.ErgoErrorResponse"],failure)));
}
throw new Error("[Ergo] " + message);
}
throw new Error("[Ergo] " + message);
}

3 changes: 3 additions & 0 deletions examples/initemittest/contract.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"$class": "org.accordproject.initemittest.TemplateModel"
}
24 changes: 24 additions & 0 deletions examples/initemittest/logic.ergo
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace org.accordproject.initemittest

contract InitEmitTest over TemplateModel state State {
clause init() : Response {
set state State {};
emit Greeting{ message: "Voila!" };
return Response{
}
}
}
35 changes: 35 additions & 0 deletions examples/initemittest/model.cto
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace org.accordproject.initemittest

concept State {
}

transaction Request {
}

transaction Response {
}

event Greeting {
o String message
}

/**
* The template model
*/
@AccordTemplateModel("initemittest")
concept TemplateModel {
}
3 changes: 3 additions & 0 deletions examples/initemittest/request.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"$class": "org.accordproject.initemittest.Request"
}
2 changes: 2 additions & 0 deletions examples/initemittest/state.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{ "$class": "org.accordproject.cicero.contract.AccordContractState",
"stateId" : "1" }
36 changes: 2 additions & 34 deletions mechanization/Translation/ErgoNNRCtoCicero.v
Original file line number Diff line number Diff line change
Expand Up @@ -80,23 +80,7 @@ Section ErgoNNRCtoCicero.
+++ `"function " +++ `fun_name +++ `"(context) {" +++ eol
+++ `" let pcontext = { '" +++ `request_param +++ `"' : context.request, 'state': " +++ state_init +++ `", 'contract': context.contract, 'emit': context.emit, 'now': context.now};" +++ eol
+++ `" //logger.info('ergo context: '+JSON.stringify(pcontext))" +++ eol
+++ `" let result = new " +++ `ErgoCodeGen.javascript_identifier_sanitizer contract_name +++ `"()." +++ `ErgoCodeGen.javascript_identifier_sanitizer clause_name +++ `"(pcontext);" +++ eol
+++ `" if (result.hasOwnProperty('left')) {" +++ eol
+++ `" //logger.info('ergo result: '+JSON.stringify(result))" +++ eol
+++ `" context.response = result.left.response ?" +++ eol
+++ `" result.left.response" +++ eol
+++ `" : { '$class': 'org.accordproject.cicero.runtime.Response' };" +++ eol
+++ `" context.state = result.left.state;" +++ eol
+++ `" let emitResult = [];" +++ eol
+++ `" for (let i = 0; i < result.left.emit.length; i++) {" +++ eol
+++ `" emitResult.push(result.left.emit[i]);" +++ eol
+++ `" }" +++ eol
+++ `" context.emit = emitResult;" +++ eol
+++ `" return context;" +++ eol
+++ `" } else {" +++ eol
+++ `" //logger.error('ergo error: '+JSON.stringify(result.right))" +++ eol
+++ `" ciceroError(result);" +++ eol
+++ `" }" +++ eol
+++ `" return new " +++ `ErgoCodeGen.javascript_identifier_sanitizer contract_name +++ `"()." +++ `ErgoCodeGen.javascript_identifier_sanitizer clause_name +++ `"(pcontext);" +++ eol
+++ `"}" +++ eol.

Definition wrapper_function_for_init
Expand All @@ -123,23 +107,7 @@ Section ErgoNNRCtoCicero.
+++ `"function " +++ `fun_name +++ `"(context) {" +++ eol
+++ `" let pcontext = { 'state': " +++ state_init +++ `", 'contract': context.contract, 'emit': context.emit, 'now': context.now};" +++ eol
+++ `" //logger.info('ergo context: '+JSON.stringify(pcontext))" +++ eol
+++ `" let result = new " +++ `ErgoCodeGen.javascript_identifier_sanitizer contract_name +++ `"()." +++ `ErgoCodeGen.javascript_identifier_sanitizer clause_name +++ `"(pcontext);" +++ eol
+++ `" if (result.hasOwnProperty('left')) {" +++ eol
+++ `" //logger.info('ergo result: '+JSON.stringify(result))" +++ eol
+++ `" context.response = result.left.response ?" +++ eol
+++ `" result.left.response" +++ eol
+++ `" : { '$class': 'org.accordproject.cicero.runtime.Response' };" +++ eol
+++ `" context.state = result.left.state;" +++ eol
+++ `" let emitResult = [];" +++ eol
+++ `" for (let i = 0; i < result.left.emit.length; i++) {" +++ eol
+++ `" emitResult.push(result.left.emit[i]);" +++ eol
+++ `" }" +++ eol
+++ `" context.emit = emitResult;" +++ eol
+++ `" return context;" +++ eol
+++ `" } else {" +++ eol
+++ `" //logger.error('ergo error: '+JSON.stringify(result.right))" +++ eol
+++ `" ciceroError(result);" +++ eol
+++ `" }" +++ eol
+++ `" return new " +++ `ErgoCodeGen.javascript_identifier_sanitizer contract_name +++ `"()." +++ `ErgoCodeGen.javascript_identifier_sanitizer clause_name +++ `"(pcontext);" +++ eol
+++ `"}" +++ eol.

Definition apply_wrapper_function
Expand Down
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3,322 changes: 1,661 additions & 1,661 deletions packages/ergo-cli/extracted/ergoccore.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/ergo-cli/extracted/ergotopcore.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3,856 changes: 1,928 additions & 1,928 deletions packages/ergo-compiler/extracted/compilercore.js

Large diffs are not rendered by default.

24 changes: 16 additions & 8 deletions packages/ergo-compiler/lib/templatelogic.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,21 +91,24 @@ class TemplateLogic {
if (target === 'cicero') {
this.getScriptManager().hasDispatch();
code = `
__dispatch({contract:data,state:state,emit:[],now:now,request:request});
const __result = __dispatch({contract:data,state:state,emit:[],now:now,request:request});
unwrapError(__result);
`;
} else if (target === 'es6') {
if (this.getContractName()) {
const contractName = ErgoCompiler.contractCallName(this.getContractName());
code = `
let contract = new ${contractName}();
contract.main({contract:data,state:state,emit:[],now:now,request:request});
const __result = contract.main({contract:data,state:state,emit:[],now:now,request:request});
unwrapError(__result);
`;
} else {
throw new Error(`Cannot create dispatch call for target: ${target} without a contract name`);
}
} else if (target === 'es5') {
code = `
main({contract:data,state:state,emit:[],now:now,request:request});
const __result = main({contract:data,state:state,emit:[],now:now,request:request});
unwrapError(__result);
`;
} else {
throw new Error(`Unsupported target: ${target}`);
Expand All @@ -124,21 +127,24 @@ main({contract:data,state:state,emit:[],now:now,request:request});
if (target === 'cicero') {
this.getScriptManager().hasDispatch();
code = `
__init({contract:data,emit:[],now:now,request:null});
const __result = __init({contract:data,emit:[],now:now,request:null});
unwrapError(__result);
`;
} else if (target === 'es6') {
if (this.getContractName()) {
const contractName = ErgoCompiler.contractCallName(this.getContractName());
code = `
let contract = new ${contractName}();
contract.init({contract:data,emit:[],now:now,request:null});
const __result = contract.init({contract:data,emit:[],now:now,request:null});
unwrapError(__result);
`;
} else {
throw new Error(`Cannot create init call for target: ${target} without a contract name`);
}
} else if (target === 'es5') {
code = `
init({contract:data,emit:[],now:now,request:null});
const __result = init({contract:data,emit:[],now:now,request:null});
unwrapError(__result);
`;
} else {
throw new Error(`Unsupported target: ${target}`);
Expand All @@ -162,14 +168,16 @@ init({contract:data,emit:[],now:now,request:null});
const contractName = ErgoCompiler.contractCallName(this.getContractName());
code = `
let contract = new ${contractName}();
contract.${clauseName}(Object.assign({}, {contract:data,state:state,emit:[],now:now} ,params));
const __result = contract.${clauseName}(Object.assign({}, {contract:data,state:state,emit:[],now:now} ,params));
unwrapError(__result);
`;
} else {
throw new Error(`Cannot create invoke call for target: ${target} without a contract name`);
}
} else if (target === 'es5') {
code = `
${clauseName}(Object.assign({}, {contract:data,state:state,emit:[],now:now} ,params));; // Create the clause call
const __result = ${clauseName}(Object.assign({}, {contract:data,state:state,emit:[],now:now} ,params));
unwrapError(__result);
`;
} else {
throw new Error(`Unsupported target: ${target}`);
Expand Down
6 changes: 3 additions & 3 deletions packages/ergo-compiler/test/scriptmanager.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ describe('ScriptManager', () => {
scriptManager.getLogic().map(x => x.name).should.deep.equal(['test.ergo']);
scriptManager.allFunctionDeclarations().length.should.equal(1);
scriptManager.allFunctionDeclarations().map(x => x.getName()).should.deep.equal(['paymentClause']);
scriptManager.getCompiledScript().getContents().length.should.equal(27739);
scriptManager.getCompiledScript().getContents().length.should.equal(26120);
scriptManager.allFunctionDeclarations().length.should.equal(98);
scriptManager.allFunctionDeclarations().filter(x => x.name === '__init').length.should.equal(1);
expect(scriptManager.hasInit()).to.not.throw;
Expand All @@ -68,8 +68,8 @@ describe('ScriptManager', () => {
const script2 = scriptManager.createScript('test.ergo','.ergo',ergoSample);
scriptManager.addScript(script1);
scriptManager.addScript(script2);
scriptManager.compileLogic().getContents().length.should.equal(27739);
scriptManager.getCompiledScript().getContents().length.should.equal(27739);
scriptManager.compileLogic().getContents().length.should.equal(26120);
scriptManager.getCompiledScript().getContents().length.should.equal(26120);
scriptManager.getAllScripts().length.should.equal(3);
});

Expand Down
Loading

0 comments on commit ccca56d

Please sign in to comment.