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

Commit

Permalink
Merge 1908e2f into 19518b7
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromesimeon committed Mar 26, 2019
2 parents 19518b7 + 1908e2f commit 336e2ee
Show file tree
Hide file tree
Showing 67 changed files with 6,362 additions and 6,024 deletions.
6 changes: 6 additions & 0 deletions examples/initemittest/logic.ergo
Expand Up @@ -21,4 +21,10 @@ contract InitEmitTest over TemplateModel state State {
return Response{
}
}
clause greet(request:Request) : Response {
set state State {};
emit Greeting{ message: "Voila!" };
return Response{
}
}
}
3 changes: 1 addition & 2 deletions examples/initemittest/state.json
@@ -1,2 +1 @@
{ "$class": "org.accordproject.cicero.contract.AccordContractState",
"stateId" : "1" }
{ "$class": "org.accordproject.initemittest.State" }
17 changes: 15 additions & 2 deletions extraction/compilerJS.ml
Expand Up @@ -103,13 +103,23 @@ let json_of_result res =
val error = json_of_ergo_success ()
val result = Js.string res
val code = Js.bool false
val contractName = Js.null
end

let json_of_result_with_contract_name cn res =
object%js
val error = json_of_ergo_success ()
val result = Js.string res
val code = Js.bool false
val contractName = Js.some (Js.string cn)
end

let json_of_error gconf error =
object%js
val error = json_of_ergo_error gconf error
val result = Js.string ""
val code = Js.bool true
val contractName = Js.null
end

let ergo_compile input =
Expand All @@ -118,9 +128,12 @@ let ergo_compile input =
let gconf = global_config_of_json gconf input in
let target_lang = ErgoConfig.get_target_lang gconf in
let all_modules = ErgoConfig.get_all_sorted gconf in
let (file,res) = ErgoCompile.ergo_compile target_lang all_modules in
let (contract_name,file,res) = ErgoCompile.ergo_compile target_lang all_modules in
let res = ErgoCompile.ergo_link gconf res in
json_of_result res
begin match contract_name with
| None -> json_of_result res
| Some cn -> json_of_result_with_contract_name cn res
end
with
| Ergo_Error error -> json_of_error gconf error
| exn -> json_of_error gconf (ergo_system_error (Printexc.to_string exn))
Expand Down
10 changes: 8 additions & 2 deletions extraction/src/ErgoCompile.ml
Expand Up @@ -20,8 +20,14 @@ open ErgoConfig
open PrettyIL

let res_convert code =
let contract_name =
begin match code.res_contract_name with
| None -> None
| Some cn -> Some (string_of_char_list cn)
end
in
(* Printf.printf "NNRC Module: %s" (pretty_nnrc_module false 0 false (Jarray []) false code.res_nnrc); *)
(string_of_char_list code.res_file, code.res_content)
(contract_name, string_of_char_list code.res_file, code.res_content)

let compile_module_to_javascript version inputs =
let code = ErgoCompiler.ergo_module_to_javascript version inputs in
Expand Down Expand Up @@ -75,7 +81,7 @@ let print_monitor source_file =
let ergo_proc gconf inputs =
let target_lang = ErgoConfig.get_target_lang gconf in
let ext = extension_of_lang target_lang in
let (source_file,result) = ergo_compile target_lang inputs in
let (contract_name,source_file,result) = ergo_compile target_lang inputs in
Printf.printf "Compiling Ergo '%s' -- " source_file;
let result = ergo_link gconf result in
print_generate source_file ext result;
Expand Down
11 changes: 7 additions & 4 deletions mechanization/Compiler/ErgoDriver.v
Expand Up @@ -269,7 +269,7 @@ Section ErgoDriver.
eolift (fun init : laergo_module * compilation_context =>
let (p, ctxt) := init in
let res := ergo_module_to_javascript version ctxt p in
elift (fun xy => mkResultFile p.(module_file) (fst xy) (snd xy)) res)
elift (fun xy => mkResultFile None p.(module_file) (fst xy) (snd xy)) res)
cinit) bm.

Definition ergo_module_to_java_top
Expand All @@ -281,7 +281,7 @@ Section ErgoDriver.
eolift (fun init : laergo_module * compilation_context =>
let (p, ctxt) := init in
let res := ergo_module_to_java ctxt p in
elift (fun xy => mkResultFile p.(module_file) (fst xy) (string_to_estring (snd xy))) res)
elift (fun xy => mkResultFile None p.(module_file) (fst xy) (string_to_estring (snd xy))) res)
cinit) bm.

Definition ergo_module_to_cicero_top
Expand All @@ -302,10 +302,13 @@ Section ErgoDriver.
let sigs := lookup_contract_signatures (snd c) in
let pc := ergo_module_to_ergoct ctxt p in
let pn := eolift (fun xy => ergoct_module_to_nnrc (fst xy)) pc in
elift (fun x => (x,ergoc_module_to_cicero contract_name (snd c).(contract_state) sigs x)) pn)
elift (fun x => (contract_name, x,ergoc_module_to_cicero contract_name (snd c).(contract_state) sigs x)) pn)
ec
in
elift (fun xy => mkResultFile p.(module_file) (fst xy) (snd xy)) res)
elift (fun xyz =>
let '(contract_name, nmod, ncontent) := xyz in
mkResultFile (Some contract_name) p.(module_file) nmod ncontent)
res)
ctxt) bm.

End CompilerTop.
Expand Down
3 changes: 2 additions & 1 deletion mechanization/ErgoNNRC/Lang/ErgoNNRC.v
Expand Up @@ -64,11 +64,12 @@ Section ErgoNNRC.

Record result_file :=
mkResultFile {
res_contract_name : option string;
res_file : string;
res_nnrc : nnrc_module;
res_content : estring;
}.

Section Semantics.
(* XXX Nothing yet -- relational semantics should go here *)
End Semantics.
Expand Down
24 changes: 6 additions & 18 deletions mechanization/Translation/ErgoNNRCtoCicero.v
Expand Up @@ -67,7 +67,6 @@ Section ErgoNNRCtoCicero.
(clause_name:string)
(eol:estring)
(quotel:estring) : estring :=
let state_init := `"context.state" in
(accord_annotation
generated
clause_name
Expand All @@ -78,7 +77,7 @@ Section ErgoNNRCtoCicero.
eol
quotel)
+++ `"function " +++ `fun_name +++ `"(context) {" +++ eol
+++ `" let pcontext = { '" +++ `request_param +++ `"' : context.request, 'state': " +++ state_init +++ `", 'contract': context.contract, 'emit': context.emit, 'now': context.now};" +++ eol
+++ `" let pcontext = { '" +++ `request_param +++ `"' : context.request, 'state': context.state, 'contract': context.contract, 'emit': context.emit, 'now': context.now};" +++ eol
+++ `" //logger.info('ergo context: '+JSON.stringify(pcontext))" +++ eol
+++ `" return new " +++ `ErgoCodeGen.javascript_identifier_sanitizer contract_name +++ `"()." +++ `ErgoCodeGen.javascript_identifier_sanitizer clause_name +++ `"(pcontext);" +++ eol
+++ `"}" +++ eol.
Expand All @@ -93,22 +92,11 @@ Section ErgoNNRCtoCicero.
(eol:estring)
(quotel:estring) : estring :=
let state_init := `"{ '$class': 'org.accordproject.cicero.contract.AccordContractState', 'stateId' : 'org.accordproject.cicero.contract.AccordContractState#1' }" in
let request_type := "org.accordproject.cicero.runtime.Request" in
let clause_name := "init" in
(accord_annotation
generated
clause_name
request_type
response_type
emit_type
state_type
eol
quotel)
+++ `"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
+++ `" return new " +++ `ErgoCodeGen.javascript_identifier_sanitizer contract_name +++ `"()." +++ `ErgoCodeGen.javascript_identifier_sanitizer clause_name +++ `"(pcontext);" +++ eol
+++ `"}" +++ eol.
`"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
+++ `" return new " +++ `ErgoCodeGen.javascript_identifier_sanitizer contract_name +++ `"().init(pcontext);" +++ eol
+++ `"}" +++ eol.

Definition apply_wrapper_function
(contract_name:string)
Expand Down
2 changes: 1 addition & 1 deletion packages/ergo-cli/.cucumber.js
@@ -1 +1 @@
require('@accordproject/ergo-engine/lib/steps');
require('@accordproject/ergo-test').Steps;
6,517 changes: 3,260 additions & 3,257 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.

4 changes: 2 additions & 2 deletions packages/ergo-cli/lib/commands.js
Expand Up @@ -76,7 +76,7 @@ class Commands {
}
let initResponse;
if (stateInput === null) {
initResponse = engine.compileAndInit(templateLogic, contractName, contractJson, currentTime);
initResponse = engine.compileAndInit(templateLogic, contractName, contractJson, {}, currentTime);
} else {
const stateJson = getJson(stateInput);
initResponse = Promise.resolve({ state: stateJson });
Expand Down Expand Up @@ -155,7 +155,7 @@ class Commands {
}
const contractJson = getJson(contractInput);
const clauseParams = getJson(paramsInput);
return engine.compileAndInit(templateLogic,contractName,contractJson,currentTime,clauseParams);
return engine.compileAndInit(templateLogic,contractName,contractJson,clauseParams,currentTime);
}

/**
Expand Down
1 change: 1 addition & 0 deletions packages/ergo-cli/package.json
Expand Up @@ -30,6 +30,7 @@
"dependencies": {
"@accordproject/ergo-compiler": "0.7.3",
"@accordproject/ergo-engine": "0.7.3",
"@accordproject/ergo-test": "0.7.3",
"moment-mini": "2.22.1",
"jsome": "2.5.0",
"winston": "^3.2.1",
Expand Down
4,773 changes: 2,389 additions & 2,384 deletions packages/ergo-compiler/extracted/compilercore.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/ergo-compiler/lib/compiler.js
Expand Up @@ -88,7 +88,7 @@ class Compiler {
if (compiled.code) {
return { 'error' : compiled.error };
} else {
return { 'success' : compiled.result };
return { 'success' : compiled.result, 'contractName' : compiled.contractName };
}
}

Expand Down
19 changes: 14 additions & 5 deletions packages/ergo-compiler/lib/script.js
Expand Up @@ -35,10 +35,12 @@ class Script {
* @param {string} identifier - The identifier of the script
* @param {string} language - The language type of the script
* @param {string} contents - The contents of the script
* @param {string} contractName - The name of the contract if known or null
*/
constructor(modelManager, identifier, language, contents) {
constructor(modelManager, identifier, language, contents, contractName) {
this.modelManager = modelManager;
this.identifier = identifier;
this.contractName = contractName;
this.language = language;
this.contents = contents;
this.functions = [];
Expand Down Expand Up @@ -74,6 +76,13 @@ class Script {
}

this.tokens = parser.getTokens();

if (!this.getContractName()) {
let classNames = parser.getClasses().map(x => x.name);
if (classNames.length !== 0) {
this.contractName = classNames[0];
}
}
}
}

Expand All @@ -97,11 +106,11 @@ class Script {
}

/**
* Returns the identifier of the script
* @return {string} the identifier of the script
* Returns the name of the contract for this script
* @return {string} the name of the contract, if known
*/
getName() {
return this.identifier;
getContractName() {
return this.contractName;
}

/**
Expand Down
65 changes: 52 additions & 13 deletions packages/ergo-compiler/lib/scriptmanager.js
Expand Up @@ -149,6 +149,23 @@ class ScriptManager {
return result;
}

/**
* Get a single combined Script
* @return {string} The source for all Scripts registered, including compiled ones
* @private
*/
getCombinedScripts() {
let allJsScripts = '';

this.getAllScripts().forEach(function (element) {
if (element.getLanguage() === '.js') {
allJsScripts += element.getContents();
}
}, this);

return allJsScripts;
}

/**
* Get the array of Script instances for the given language
* @param {string} language - The scripts' language
Expand Down Expand Up @@ -176,13 +193,13 @@ class ScriptManager {
let logic = [];
const scripts = this.getScriptsForLanguage('.ergo');
scripts.forEach(function (script) {
logic.push({ 'name' : script.getName(), 'content' : script.getContents() });
logic.push({ 'name' : script.getIdentifier(), 'content' : script.getContents() });
});
return logic;
}

/**
* Remove all registered Composer files
* Remove all registered scripts
*/
clearScripts() {
this.scripts = {};
Expand All @@ -200,15 +217,32 @@ class ScriptManager {
}

/**
* Get the Script associated with an identifier
* @param {string} identifier - the identifier of the Script
* Get the compiled Script
* @return {Script} the Script
* @private
*/
getCompiledScript() {
return this.compileLogic(false);
}

/**
* Get the compiled JavaScript
* @return {string} the Script
* @private
*/
getCompiledJavaScript() {
const compiledScript = this.compiledScript;
let allJsScripts = '';

if (compiledScript) {
allJsScripts += compiledScript.getContents();
} else {
throw new Error('Did not find any compiled JavaScript logic');
}

return allJsScripts;
}

/**
* Get the identifiers of all registered scripts
* @return {string[]} The identifiers of all registered scripts
Expand All @@ -226,18 +260,23 @@ class ScriptManager {
if (this.compiledScript && !force) {
return this.compiledScript;
}
const codeExt = this.target === 'java' ? '.java' : '.js';
let sourceErgo = this.getLogic();
if (sourceErgo === undefined || sourceErgo.length === 0) {
return null;
}
// Do not link to runtime for Java target, only for JavaScript
const link = this.target === 'java' ? false : true;
const compiledErgo = ErgoCompiler.compileToJavaScript(sourceErgo,this.modelManager.getModels(),this.target,link);
if (compiledErgo.hasOwnProperty('error')) {
throw new Error(ErgoCompiler.ergoVerboseErrorToString(compiledErgo.error));
const allJsScripts = this.getCombinedScripts();
if (allJsScripts === '') {
return null;
}
this.compiledScript = new Script(this.modelManager, 'main'+codeExt, codeExt, allJsScripts, null);
} else {
// Do not link to runtime for Java target, only for JavaScript
const link = this.target === 'java' ? false : true;
const compiledErgo = ErgoCompiler.compileToJavaScript(sourceErgo,this.modelManager.getModels(),this.target,link);
if (compiledErgo.hasOwnProperty('error')) {
throw new Error(ErgoCompiler.ergoVerboseErrorToString(compiledErgo.error));
}
this.compiledScript = new Script(this.modelManager, 'main'+codeExt, codeExt, compiledErgo.success, compiledErgo.contractName);
}
const codeExt = this.target === 'java' ? '.java' : '.js';
this.compiledScript = new Script(this.modelManager, 'main'+codeExt, codeExt, compiledErgo.success);
return this.compiledScript;
}

Expand Down

0 comments on commit 336e2ee

Please sign in to comment.