bdcontract-doc/markdown_trans/markdown_ZRYH/YJSInDepth.md
2022-08-05 18:29:30 +08:00

7.4 KiB
Raw Permalink Blame History

YJS grammar


An overview of the

YJS source files include any number of The import declaration and one The contract is defined.


The import declaration

Like JavaScript (ES6), YJS also supports import statements. At the global level, developers can use the following import statements to import other files.

import "filename";

content

The import statement imports all global symbols (units) contained in the “filename” file into the current file and is globally valid.

The path

filename Usually / is used as the directory separator to indicate the file path. For example, to import the x.yjs file from the same directory to the current file, you can use the import “x.yjs” statement. Import x.yjs from another directory using the import “lib/x.yjs” statement.


The Contract is defined

The sample

Here is an example contract for JSON processing, with the YJS source file named after the contract name.

contract ScoreAdder{
	//arg =  {"action":"main","arg":"[{\"score\":20},{\"score\":20}]"}
	export function main(arg){
		//JSON is a build-in object.
		var point = JSON.parse(arg);
		var s = 0;
		print(point[0].score);
		print(point.length);
		for (var i=0;i<point.length;i++){
			s+=point[i].score/1.0;
		}
		print("total score= "+s);
		return s;
	}
}

annotation

YJS source files support single-line comments represented by (//) and multi-line comments represented by (/*...*/), as shown below:

// 这是一个单行注释
/*
 * 这是一个
 * 多行注释
 */

annotations

Like Java, YJS also supports annotations to declare contracts and functions.

In YJS, there are several built-in annotations: LogType, LogLocation, Access, Permission. Of course, you can customize your annotations. The detailed usage of the built-in annotations can be viewed through the YJS built-in API documentation. Developers can use the following annotations to declare contracts and functions.

@LogType("Arg","Result","Branch")
@SelfDefinedAnntation("dad",1)
@Access
function xxx(){}

structure

Contracts in YJS are similar to classes in Java. Each contract defines a certain number of variable, function and The event.

variable

Variables in YJS are similar to those in JavaScript (ES6), which are divided into The global variable and A local variable.

Global variables:

judge =  true;

Local variables:

var vs = "This is a string";

All variables are Dynamic type because the specific type of variable is determined by the value of the variable. In the example above, the global variable judge is of type bool and the local variable VS is of type string.

function

A function is an executable unit in a contract. There are two types of functions in YJS: ordinary functions and exported functions decorated with the export keyword.

The following is the difference between exported functions and ordinary functions:

  1. Only the exported function can be called by other contracts.
  2. Exported functions can only have one parameter and the parameter type must be String.
  3. Exported functions return type must be String.
  4. The built-in object requester (the requesters public key) can only exist in the exported function or the onCreate function, because onCreate(), although not modified by the export keyword, comes with a Requester object and the function can be executed automatically.

The event

Events in YJS are similar to events in Solidity.

The publisher Define an event that contains the event publisher function, and then publish the event by calling the event publisher function.

The subscriber Subscribe and process events.

The following is an example of an event:

EventPuber.yjs

contract EventPuber{
	event abcEvent;
  	export function pub(arg){
		abcEvent(arg);
  		return "done!";
	}
}

EventSuber.yjs

contract EventSuber{
  	export function init(arg){
		YancloudUtil.subscribe("EventPuber","abcEvent",handler);
        print("Handler:"+handler);
	}
  	function handler(e){
        var ret = "ReceiveEvent:"+(e.type)+" "+(e.content);
		print(ret);
	}
}

YJS project

In addition to the YJS source file that contains only a contract definition, the YJS engine also supports YJS project.

Each YJS project contains various files used by the execution of a contract, including YJS source file files and other resources, such as “mainfest.json”, “.js”, “.txt”, “.jar”,...

Manifest.json

Each YJS project must have a mainfest.json file in the root directory of the project, which describes the necessary information required by the contract for the YJS compilation tool (YJS engine).

Structure of the Manifest

The manifest file must contain the following information:

  1. main: contract document to be executed in the project.
  2. type: type of contract, such as data contract/algorithm contract...
  3. builder: Name of the developer who built the YJS project.
  4. insnLimit: indicates the value consumed to run the contract.
  5. pyDependences: Python dependencies required by the project.

The Manifest sample

{
	"main": "contract.js",
	"type": "Data",
	"builder": "caihq",
	"permissions": 0L,
	"pyDependences": [
        {
            "name": "yjsexample",
            "modules": [
                {
                    "name": "sample"
                }
            ]
        }
    ]
}

YJS-Java

Jar file implements Calling across languages between YJS and other programming languages, such as yjs-Java, yjs-Python,... The contract can call methods directly in Java by wrapping Java files into JAR packages.

Java class:

package your.own.pkg;
public class HelloWorld {
 public static int fun(String arg){
  return arg.length;
 }
 public int fun2(String arg){
  return arg.length;
 }
}

InvokeJava.yjs

contract InvokeJava{
 export function main(arg){
  var Hello = Java.type("your.own.package.HelloWorld");
  var hello = new Hello();
  return Hello.fun(arg)+hello.fun2(arg);
 }
}

YJS — front end

Using the Surui client to access smart contracts supports fetching resource files such as HTML/JS/CSS from smart contracts and rendering them in BDWareWebClient. Start by importing the following modules into the contract.

module Viewable{
  export function loadResource(arg){
    return Global.Resources.loadAsString(arg);
  }
  export function needRender(arg){
    return true;
  }
}

Also, after the import, define a getMainFrame method so that the mainframe can be recognized by the mainframe client:

export function getMainFrame(arg){
  return "/html/main.html";
}

The result of this method is the path to a resource file. The example resource file ”/ HTML /main.html” is as follows:

<div>
<button onclick="queryDataFromContract()">Hello,</button> Data from contract:
<span id="resultText"></span>
<script fromContract="/html/hello.js"></script>
<link fromContract="/html/hello.css"/>
</div>

The example resource file ”/ HTML /hello.js” is as follows:

var queryDataFromContract = function(){
    //第一个参数为函数名,第二个为参数,第三个参数为回调。
	var data = executeCurrentContract("query","abc",function(argg){
    	$("#resultText")[0].innerHTML = argg.result;
    });
}

Reference example:

YJS-Python

TODO