500 lines
28 KiB
HTML
500 lines
28 KiB
HTML
|
||
|
||
<!DOCTYPE html>
|
||
<html class="writer-html5" lang="zh-CN" >
|
||
<head>
|
||
<meta charset="utf-8" />
|
||
|
||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||
|
||
<title>YJS语法 — 北大数瑞大数据区块链 V1.0 文档</title>
|
||
|
||
|
||
|
||
<link rel="stylesheet" href="_static/css/theme.css" type="text/css" />
|
||
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
|
||
|
||
|
||
|
||
<link rel="shortcut icon" href="_static/favicon.ico"/>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<!--[if lt IE 9]>
|
||
<script src="_static/js/html5shiv.min.js"></script>
|
||
<![endif]-->
|
||
|
||
|
||
<script type="text/javascript" id="documentation_options" data-url_root="./" src="_static/documentation_options.js"></script>
|
||
<script src="_static/jquery.js"></script>
|
||
<script src="_static/underscore.js"></script>
|
||
<script src="_static/doctools.js"></script>
|
||
<script src="_static/translations.js"></script>
|
||
|
||
<script type="text/javascript" src="_static/js/theme.js"></script>
|
||
|
||
|
||
<link rel="index" title="索引" href="genindex.html" />
|
||
<link rel="search" title="搜索" href="search.html" />
|
||
<link rel="next" title="YJS SDK" href="YJSAPI.html" />
|
||
<link rel="prev" title="BDContract SDK" href="ContractAPI.html" />
|
||
</head>
|
||
|
||
<body class="wy-body-for-nav">
|
||
|
||
|
||
<div class="wy-grid-for-nav">
|
||
|
||
<nav data-toggle="wy-nav-shift" class="wy-nav-side">
|
||
<div class="wy-side-scroll">
|
||
<div class="wy-side-nav-search" >
|
||
|
||
|
||
|
||
<a href="index.html">
|
||
|
||
|
||
|
||
|
||
<img src="_static/logo.png" class="logo" alt="Logo"/>
|
||
|
||
</a>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<div role="search">
|
||
<form id="rtd-search-form" class="wy-form" action="search.html" method="get">
|
||
<input type="text" name="q" placeholder="在文档中搜索" />
|
||
<input type="hidden" name="check_keywords" value="yes" />
|
||
<input type="hidden" name="area" value="default" />
|
||
</form>
|
||
</div>
|
||
|
||
|
||
</div>
|
||
|
||
|
||
<div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<p class="caption"><span class="caption-text">目录</span></p>
|
||
<ul class="current">
|
||
<li class="toctree-l1"><a class="reference internal" href="Introduction.html">BDContract介绍</a></li>
|
||
<li class="toctree-l1"><a class="reference internal" href="InstallTips.html">BDContract安装说明</a></li>
|
||
<li class="toctree-l1"><a class="reference internal" href="IDEUsage.html">BDContract管理界面</a></li>
|
||
<li class="toctree-l1"><a class="reference internal" href="ContractAPI.html">BDContract SDK</a></li>
|
||
<li class="toctree-l1 current"><a class="current reference internal" href="#">YJS语法</a><ul>
|
||
<li class="toctree-l2"><a class="reference internal" href="#id1">概述</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="#import">import声明</a><ul>
|
||
<li class="toctree-l3"><a class="reference internal" href="#id2">内容</a></li>
|
||
<li class="toctree-l3"><a class="reference internal" href="#id3">路径</a></li>
|
||
</ul>
|
||
</li>
|
||
<li class="toctree-l2"><a class="reference internal" href="#contract">Contract定义</a><ul>
|
||
<li class="toctree-l3"><a class="reference internal" href="#id4">示例</a></li>
|
||
<li class="toctree-l3"><a class="reference internal" href="#id5">注释</a></li>
|
||
<li class="toctree-l3"><a class="reference internal" href="#id6">注解</a></li>
|
||
<li class="toctree-l3"><a class="reference internal" href="#id7">结构</a><ul>
|
||
<li class="toctree-l4"><a class="reference internal" href="#id8">变量</a></li>
|
||
<li class="toctree-l4"><a class="reference internal" href="#id9">函数</a></li>
|
||
<li class="toctree-l4"><a class="reference internal" href="#id10">事件</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
</li>
|
||
<li class="toctree-l2"><a class="reference internal" href="#id11">YJS项目</a><ul>
|
||
<li class="toctree-l3"><a class="reference internal" href="#manifest-json">Manifest.json</a><ul>
|
||
<li class="toctree-l4"><a class="reference internal" href="#manifest">Manifest结构</a></li>
|
||
<li class="toctree-l4"><a class="reference internal" href="#id12">Manifest示例</a></li>
|
||
</ul>
|
||
</li>
|
||
<li class="toctree-l3"><a class="reference internal" href="#yjs-java">YJS-Java</a></li>
|
||
<li class="toctree-l3"><a class="reference internal" href="#id13">YJS-前端</a></li>
|
||
<li class="toctree-l3"><a class="reference internal" href="#yjs-python">YJS-Python</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
</li>
|
||
<li class="toctree-l1"><a class="reference internal" href="YJSAPI.html">YJS SDK</a></li>
|
||
</ul>
|
||
|
||
|
||
|
||
</div>
|
||
|
||
</div>
|
||
</nav>
|
||
|
||
<section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">
|
||
|
||
|
||
<nav class="wy-nav-top" aria-label="top navigation">
|
||
|
||
<i data-toggle="wy-nav-top" class="fa fa-bars"></i>
|
||
<a href="index.html">北大数瑞大数据区块链</a>
|
||
|
||
</nav>
|
||
|
||
|
||
<div class="wy-nav-content">
|
||
|
||
<div class="rst-content">
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<div role="navigation" aria-label="breadcrumbs navigation">
|
||
|
||
<ul class="wy-breadcrumbs">
|
||
|
||
<li><a href="index.html" class="icon icon-home"></a> »</li>
|
||
|
||
<li>YJS语法</li>
|
||
|
||
|
||
<li class="wy-breadcrumbs-aside">
|
||
|
||
|
||
<a href="_sources/YJSInDepth.rst.txt" rel="nofollow"> 查看页面源码</a>
|
||
|
||
|
||
</li>
|
||
|
||
</ul>
|
||
|
||
|
||
<hr/>
|
||
</div>
|
||
<div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
|
||
<div itemprop="articleBody">
|
||
|
||
<div class="section" id="yjs">
|
||
<h1>YJS语法<a class="headerlink" href="#yjs" title="永久链接至标题">¶</a></h1>
|
||
<hr class="docutils" />
|
||
<div class="section" id="id1">
|
||
<h2>概述<a class="headerlink" href="#id1" title="永久链接至标题">¶</a></h2>
|
||
<p>YJS源文件包括任意数量的<strong>import声明</strong>和一个<strong>contract定义</strong>。</p>
|
||
</div>
|
||
<hr class="docutils" />
|
||
<div class="section" id="import">
|
||
<h2>import声明<a class="headerlink" href="#import" title="永久链接至标题">¶</a></h2>
|
||
<p>与JavaScript(ES6)类似,YJS也支持import声明语句,在全局层面,开发者可以使用如下import声明来导入其他文件。</p>
|
||
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="s2">"filename"</span><span class="p">;</span>
|
||
</pre></div>
|
||
</div>
|
||
<div class="section" id="id2">
|
||
<h3>内容<a class="headerlink" href="#id2" title="永久链接至标题">¶</a></h3>
|
||
<p>import声明语句将包含在“filename”文件中的所有全局符号(单元)导入到当前文件,且全局范围内有效。</p>
|
||
</div>
|
||
<div class="section" id="id3">
|
||
<h3>路径<a class="headerlink" href="#id3" title="永久链接至标题">¶</a></h3>
|
||
<p><strong>filename</strong>通常用<strong>/</strong>做目录分隔符来表示文件的路径,例如,从同一目录下导入<strong>x.yjs</strong>文件到当前文件,可以使用<strong>import
|
||
“x.yjs”</strong>语句;从其他目录下导入<strong>x.yjs</strong>使用<strong>import
|
||
“lib/x.yjs”</strong>语句。</p>
|
||
</div>
|
||
</div>
|
||
<hr class="docutils" />
|
||
<div class="section" id="contract">
|
||
<h2>Contract定义<a class="headerlink" href="#contract" title="永久链接至标题">¶</a></h2>
|
||
<div class="section" id="id4">
|
||
<h3>示例<a class="headerlink" href="#id4" title="永久链接至标题">¶</a></h3>
|
||
<p>以下是一个合约示例,用于JSON处理,此YJS源文件以合约名称命名。</p>
|
||
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">contract</span> <span class="n">ScoreAdder</span><span class="p">{</span>
|
||
<span class="o">//</span><span class="n">arg</span> <span class="o">=</span> <span class="p">{</span><span class="s2">"action"</span><span class="p">:</span><span class="s2">"main"</span><span class="p">,</span><span class="s2">"arg"</span><span class="p">:</span><span class="s2">"[{</span><span class="se">\"</span><span class="s2">score</span><span class="se">\"</span><span class="s2">:20},{</span><span class="se">\"</span><span class="s2">score</span><span class="se">\"</span><span class="s2">:20}]"</span><span class="p">}</span>
|
||
<span class="n">export</span> <span class="n">function</span> <span class="n">main</span><span class="p">(</span><span class="n">arg</span><span class="p">){</span>
|
||
<span class="o">//</span><span class="n">JSON</span> <span class="ow">is</span> <span class="n">a</span> <span class="n">build</span><span class="o">-</span><span class="ow">in</span> <span class="nb">object</span><span class="o">.</span>
|
||
<span class="n">var</span> <span class="n">point</span> <span class="o">=</span> <span class="n">JSON</span><span class="o">.</span><span class="n">parse</span><span class="p">(</span><span class="n">arg</span><span class="p">);</span>
|
||
<span class="n">var</span> <span class="n">s</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
|
||
<span class="nb">print</span><span class="p">(</span><span class="n">point</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">score</span><span class="p">);</span>
|
||
<span class="nb">print</span><span class="p">(</span><span class="n">point</span><span class="o">.</span><span class="n">length</span><span class="p">);</span>
|
||
<span class="k">for</span> <span class="p">(</span><span class="n">var</span> <span class="n">i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="n">i</span><span class="o"><</span><span class="n">point</span><span class="o">.</span><span class="n">length</span><span class="p">;</span><span class="n">i</span><span class="o">++</span><span class="p">){</span>
|
||
<span class="n">s</span><span class="o">+=</span><span class="n">point</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">score</span><span class="o">/</span><span class="mf">1.0</span><span class="p">;</span>
|
||
<span class="p">}</span>
|
||
<span class="nb">print</span><span class="p">(</span><span class="s2">"total score= "</span><span class="o">+</span><span class="n">s</span><span class="p">);</span>
|
||
<span class="k">return</span> <span class="n">s</span><span class="p">;</span>
|
||
<span class="p">}</span>
|
||
<span class="p">}</span>
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
<div class="section" id="id5">
|
||
<h3>注释<a class="headerlink" href="#id5" title="永久链接至标题">¶</a></h3>
|
||
<p>YJS源文件支持以(<strong>//</strong>)表示的单行注释和以(<strong>/*…*/</strong>)表示的多行注释,如下所示:</p>
|
||
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="o">//</span> <span class="n">这是一个单行注释</span>
|
||
<span class="o">/*</span>
|
||
<span class="o">*</span> <span class="n">这是一个</span>
|
||
<span class="o">*</span> <span class="n">多行注释</span>
|
||
<span class="o">*/</span>
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
<div class="section" id="id6">
|
||
<h3>注解<a class="headerlink" href="#id6" title="永久链接至标题">¶</a></h3>
|
||
<p>与Java类似,YJS也支持注解声明合约和函数。</p>
|
||
<p>在YJS中,有几种内建注解:LogType、LogLocation、Access、Permission。
|
||
当然,可以自定义注解。
|
||
内建注解的详细使用方式可以通过YJS内置API文档查看。
|
||
开发者可以使用如下注解来声明合约和函数。</p>
|
||
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="nd">@LogType</span><span class="p">(</span><span class="s2">"Arg"</span><span class="p">,</span><span class="s2">"Result"</span><span class="p">,</span><span class="s2">"Branch"</span><span class="p">)</span>
|
||
<span class="nd">@SelfDefinedAnntation</span><span class="p">(</span><span class="s2">"dad"</span><span class="p">,</span><span class="mi">1</span><span class="p">)</span>
|
||
<span class="nd">@Access</span>
|
||
<span class="n">function</span> <span class="n">xxx</span><span class="p">(){}</span>
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
<div class="section" id="id7">
|
||
<h3>结构<a class="headerlink" href="#id7" title="永久链接至标题">¶</a></h3>
|
||
<p>YJS中的合约类似于Java中的类。每个合约都定义了一定数量的<strong>变量</strong>、<strong>函数</strong>和<strong>事件</strong>。</p>
|
||
<div class="section" id="id8">
|
||
<h4>变量<a class="headerlink" href="#id8" title="永久链接至标题">¶</a></h4>
|
||
<p>YJS中的变量类似于JavaScript(ES6)中的变量,分为<strong>全局变量</strong>和<strong>局部变量</strong>。</p>
|
||
<p>全局变量:</p>
|
||
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">judge</span> <span class="o">=</span> <span class="n">true</span><span class="p">;</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>局部变量:</p>
|
||
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">var</span> <span class="n">vs</span> <span class="o">=</span> <span class="s2">"This is a string"</span><span class="p">;</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>所有的变量都是<strong>动态类型</strong>,因为变量的具体类型是根据变量值来决定的。如上示例中,全局变量judge是bool类型,局部变量vs是字符串类型。</p>
|
||
</div>
|
||
<div class="section" id="id9">
|
||
<h4>函数<a class="headerlink" href="#id9" title="永久链接至标题">¶</a></h4>
|
||
<p>函数是合约中的可执行单元。YJS中有两种类型的函数:普通函数和用<strong>export</strong>关键字修饰的exported函数。</p>
|
||
<p>以下是exported函数和普通函数的区别:</p>
|
||
<ol class="arabic simple">
|
||
<li><p>只有exported函数能被其他合约调用。</p></li>
|
||
<li><p>exported函数只能有一个参数且参数类型必须为String。</p></li>
|
||
<li><p>exported函数的返回类型必须是String。</p></li>
|
||
<li><p>内置对象requester(请求者的公钥)只能存在于exported函数或onCreate函数,因为onCreate()虽然没被<strong>export</strong>关键字修饰,但它自带requester对象且该函数可以自动执行。</p></li>
|
||
</ol>
|
||
</div>
|
||
<div class="section" id="id10">
|
||
<h4>事件<a class="headerlink" href="#id10" title="永久链接至标题">¶</a></h4>
|
||
<p>YJS中的事件类似于Solidity中的事件。</p>
|
||
<p><strong>发布者</strong>定义一个包含事件发布函数的事件,然后通过调用事件发布函数发布事件。</p>
|
||
<p><strong>订阅者</strong>订阅并处理事件。</p>
|
||
<p>事件示例如下:</p>
|
||
<p>EventPuber.yjs</p>
|
||
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">contract</span> <span class="n">EventPuber</span><span class="p">{</span>
|
||
<span class="n">event</span> <span class="n">abcEvent</span><span class="p">;</span>
|
||
<span class="n">export</span> <span class="n">function</span> <span class="n">pub</span><span class="p">(</span><span class="n">arg</span><span class="p">){</span>
|
||
<span class="n">abcEvent</span><span class="p">(</span><span class="n">arg</span><span class="p">);</span>
|
||
<span class="k">return</span> <span class="s2">"done!"</span><span class="p">;</span>
|
||
<span class="p">}</span>
|
||
<span class="p">}</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>EventSuber.yjs</p>
|
||
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">contract</span> <span class="n">EventSuber</span><span class="p">{</span>
|
||
<span class="n">export</span> <span class="n">function</span> <span class="n">init</span><span class="p">(</span><span class="n">arg</span><span class="p">){</span>
|
||
<span class="n">YancloudUtil</span><span class="o">.</span><span class="n">subscribe</span><span class="p">(</span><span class="s2">"EventPuber"</span><span class="p">,</span><span class="s2">"abcEvent"</span><span class="p">,</span><span class="n">handler</span><span class="p">);</span>
|
||
<span class="nb">print</span><span class="p">(</span><span class="s2">"Handler:"</span><span class="o">+</span><span class="n">handler</span><span class="p">);</span>
|
||
<span class="p">}</span>
|
||
<span class="n">function</span> <span class="n">handler</span><span class="p">(</span><span class="n">e</span><span class="p">){</span>
|
||
<span class="n">var</span> <span class="n">ret</span> <span class="o">=</span> <span class="s2">"ReceiveEvent:"</span><span class="o">+</span><span class="p">(</span><span class="n">e</span><span class="o">.</span><span class="n">type</span><span class="p">)</span><span class="o">+</span><span class="s2">" "</span><span class="o">+</span><span class="p">(</span><span class="n">e</span><span class="o">.</span><span class="n">content</span><span class="p">);</span>
|
||
<span class="nb">print</span><span class="p">(</span><span class="n">ret</span><span class="p">);</span>
|
||
<span class="p">}</span>
|
||
<span class="p">}</span>
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<hr class="docutils" />
|
||
<div class="section" id="id11">
|
||
<h2>YJS项目<a class="headerlink" href="#id11" title="永久链接至标题">¶</a></h2>
|
||
<p>除了只包含一个contract定义的YJS源文件,YJS引擎还支持<strong>YJS项目</strong>。</p>
|
||
<p>每个YJS
|
||
project包含了合约执行过程中需用到的各种文件,包括yjs源文件<strong>“.yjs”</strong>和其他资源文件,如<strong>“mainfest.json”</strong>,
|
||
<strong>“.js”</strong>, <strong>“.txt”</strong>,<strong>“.jar”</strong>, …</p>
|
||
<div class="section" id="manifest-json">
|
||
<h3>Manifest.json<a class="headerlink" href="#manifest-json" title="永久链接至标题">¶</a></h3>
|
||
<p>每个YJS项目在项目的根目录下必须有一个<strong>mainfest.json</strong>文件,此文件描述了合约对于YJS的编译工具(YJS引擎)所需的必要信息。</p>
|
||
<div class="section" id="manifest">
|
||
<h4>Manifest结构<a class="headerlink" href="#manifest" title="永久链接至标题">¶</a></h4>
|
||
<p>manifest文件需包含以下信息:</p>
|
||
<ol class="arabic simple">
|
||
<li><p><strong>main</strong>: 项目中将要被执行的合约文件。</p></li>
|
||
<li><p><strong>type</strong>: 合约的类型,如数据合约/算法合约…</p></li>
|
||
<li><p><strong>builder</strong>: 构建YJS项目的开发者姓名。</p></li>
|
||
<li><p><strong>insnLimit</strong>: 运行合约需要消耗的值。</p></li>
|
||
<li><p><strong>pyDependences</strong>: 项目所需的Python依赖。</p></li>
|
||
</ol>
|
||
</div>
|
||
<div class="section" id="id12">
|
||
<h4>Manifest示例<a class="headerlink" href="#id12" title="永久链接至标题">¶</a></h4>
|
||
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="p">{</span>
|
||
<span class="s2">"main"</span><span class="p">:</span> <span class="s2">"contract.js"</span><span class="p">,</span>
|
||
<span class="s2">"type"</span><span class="p">:</span> <span class="s2">"Data"</span><span class="p">,</span>
|
||
<span class="s2">"builder"</span><span class="p">:</span> <span class="s2">"caihq"</span><span class="p">,</span>
|
||
<span class="s2">"permissions"</span><span class="p">:</span> <span class="mi">0</span><span class="n">L</span><span class="p">,</span>
|
||
<span class="s2">"pyDependences"</span><span class="p">:</span> <span class="p">[</span>
|
||
<span class="p">{</span>
|
||
<span class="s2">"name"</span><span class="p">:</span> <span class="s2">"yjsexample"</span><span class="p">,</span>
|
||
<span class="s2">"modules"</span><span class="p">:</span> <span class="p">[</span>
|
||
<span class="p">{</span>
|
||
<span class="s2">"name"</span><span class="p">:</span> <span class="s2">"sample"</span>
|
||
<span class="p">}</span>
|
||
<span class="p">]</span>
|
||
<span class="p">}</span>
|
||
<span class="p">]</span>
|
||
<span class="p">}</span>
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<div class="section" id="yjs-java">
|
||
<h3>YJS-Java<a class="headerlink" href="#yjs-java" title="永久链接至标题">¶</a></h3>
|
||
<p>Jar文件实现了YJS与其他编程语言间的<strong>跨语言调用</strong>,如YJS-Java,
|
||
YJS-Python, …
|
||
通过将Java文件包成jar包的方式,使得合约可直接调用Java中的方法。</p>
|
||
<p>Java class:</p>
|
||
<div class="highlight-java notranslate"><div class="highlight"><pre><span></span><span class="kn">package</span> <span class="nn">your.own.pkg</span><span class="o">;</span>
|
||
<span class="kd">public</span> <span class="kd">class</span> <span class="nc">HelloWorld</span> <span class="o">{</span>
|
||
<span class="kd">public</span> <span class="kd">static</span> <span class="kt">int</span> <span class="nf">fun</span><span class="o">(</span><span class="n">String</span> <span class="n">arg</span><span class="o">){</span>
|
||
<span class="k">return</span> <span class="n">arg</span><span class="o">.</span><span class="na">length</span><span class="o">;</span>
|
||
<span class="o">}</span>
|
||
<span class="kd">public</span> <span class="kt">int</span> <span class="nf">fun2</span><span class="o">(</span><span class="n">String</span> <span class="n">arg</span><span class="o">){</span>
|
||
<span class="k">return</span> <span class="n">arg</span><span class="o">.</span><span class="na">length</span><span class="o">;</span>
|
||
<span class="o">}</span>
|
||
<span class="o">}</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>InvokeJava.yjs</p>
|
||
<div class="highlight-javascript notranslate"><div class="highlight"><pre><span></span><span class="nx">contract</span> <span class="nx">InvokeJava</span><span class="p">{</span>
|
||
<span class="kr">export</span> <span class="kd">function</span> <span class="nx">main</span><span class="p">(</span><span class="nx">arg</span><span class="p">){</span>
|
||
<span class="kd">var</span> <span class="nx">Hello</span> <span class="o">=</span> <span class="nx">Java</span><span class="p">.</span><span class="nx">type</span><span class="p">(</span><span class="s2">"your.own.package.HelloWorld"</span><span class="p">);</span>
|
||
<span class="kd">var</span> <span class="nx">hello</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Hello</span><span class="p">();</span>
|
||
<span class="k">return</span> <span class="nx">Hello</span><span class="p">.</span><span class="nx">fun</span><span class="p">(</span><span class="nx">arg</span><span class="p">)</span><span class="o">+</span><span class="nx">hello</span><span class="p">.</span><span class="nx">fun2</span><span class="p">(</span><span class="nx">arg</span><span class="p">);</span>
|
||
<span class="p">}</span>
|
||
<span class="p">}</span>
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
<div class="section" id="id13">
|
||
<h3>YJS-前端<a class="headerlink" href="#id13" title="永久链接至标题">¶</a></h3>
|
||
<p>使用数瑞客户端来访问智能合约支持从智能合约中获取html/js/css等资源文件,
|
||
并在<a class="reference external" href="./_static/BDWareWebClient.zip">BDWareWebClient</a>中渲染。
|
||
首先在合约中import以下模块。</p>
|
||
<div class="highlight-javascript notranslate"><div class="highlight"><pre><span></span><span class="nx">module</span> <span class="nx">Viewable</span><span class="p">{</span>
|
||
<span class="kr">export</span> <span class="kd">function</span> <span class="nx">loadResource</span><span class="p">(</span><span class="nx">arg</span><span class="p">){</span>
|
||
<span class="k">return</span> <span class="nx">Global</span><span class="p">.</span><span class="nx">Resources</span><span class="p">.</span><span class="nx">loadAsString</span><span class="p">(</span><span class="nx">arg</span><span class="p">);</span>
|
||
<span class="p">}</span>
|
||
<span class="kr">export</span> <span class="kd">function</span> <span class="nx">needRender</span><span class="p">(</span><span class="nx">arg</span><span class="p">){</span>
|
||
<span class="k">return</span> <span class="kc">true</span><span class="p">;</span>
|
||
<span class="p">}</span>
|
||
<span class="p">}</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>同时,import以后,定义一个getMainFrame方法,以便数瑞客户端识别主页:</p>
|
||
<div class="highlight-javascript notranslate"><div class="highlight"><pre><span></span><span class="kr">export</span> <span class="kd">function</span> <span class="nx">getMainFrame</span><span class="p">(</span><span class="nx">arg</span><span class="p">){</span>
|
||
<span class="k">return</span> <span class="s2">"/html/main.html"</span><span class="p">;</span>
|
||
<span class="p">}</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>该方法的返回结果为一个资源文件的路径。
|
||
示例的资源文件“/html/main.html”如下:</p>
|
||
<div class="highlight-html notranslate"><div class="highlight"><pre><span></span><span class="p"><</span><span class="nt">div</span><span class="p">></span>
|
||
<span class="p"><</span><span class="nt">button</span> <span class="na">onclick</span><span class="o">=</span><span class="s">"queryDataFromContract()"</span><span class="p">></span>Hello,<span class="p"></</span><span class="nt">button</span><span class="p">></span> Data from contract:
|
||
<span class="p"><</span><span class="nt">span</span> <span class="na">id</span><span class="o">=</span><span class="s">"resultText"</span><span class="p">></</span><span class="nt">span</span><span class="p">></span>
|
||
<span class="p"><</span><span class="nt">script</span> <span class="na">fromContract</span><span class="o">=</span><span class="s">"/html/hello.js"</span><span class="p">></</span><span class="nt">script</span><span class="p">></span>
|
||
<span class="p"><</span><span class="nt">link</span> <span class="na">fromContract</span><span class="o">=</span><span class="s">"/html/hello.css"</span><span class="p">/></span>
|
||
<span class="p"></</span><span class="nt">div</span><span class="p">></span>
|
||
</pre></div>
|
||
</div>
|
||
<p>示例的资源文件“/html/hello.js”如下:</p>
|
||
<div class="highlight-javascript notranslate"><div class="highlight"><pre><span></span><span class="kd">var</span> <span class="nx">queryDataFromContract</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(){</span>
|
||
<span class="c1">//第一个参数为函数名,第二个为参数,第三个参数为回调。</span>
|
||
<span class="kd">var</span> <span class="nx">data</span> <span class="o">=</span> <span class="nx">executeCurrentContract</span><span class="p">(</span><span class="s2">"query"</span><span class="p">,</span><span class="s2">"abc"</span><span class="p">,</span><span class="kd">function</span><span class="p">(</span><span class="nx">argg</span><span class="p">){</span>
|
||
<span class="nx">$</span><span class="p">(</span><span class="s2">"#resultText"</span><span class="p">)[</span><span class="mi">0</span><span class="p">].</span><span class="nx">innerHTML</span> <span class="o">=</span> <span class="nx">argg</span><span class="p">.</span><span class="nx">result</span><span class="p">;</span>
|
||
<span class="p">});</span>
|
||
<span class="p">}</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>参考示例:</p>
|
||
</div>
|
||
<div class="section" id="yjs-python">
|
||
<h3>YJS-Python<a class="headerlink" href="#yjs-python" title="永久链接至标题">¶</a></h3>
|
||
<p>TODO</p>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
|
||
|
||
</div>
|
||
|
||
</div>
|
||
<footer>
|
||
<div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
|
||
<a href="YJSAPI.html" class="btn btn-neutral float-right" title="YJS SDK" accesskey="n" rel="next">下一页 <span class="fa fa-arrow-circle-right" aria-hidden="true"></span></a>
|
||
<a href="ContractAPI.html" class="btn btn-neutral float-left" title="BDContract SDK" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left" aria-hidden="true"></span> 上一页</a>
|
||
</div>
|
||
|
||
<hr/>
|
||
|
||
<div role="contentinfo">
|
||
<p>
|
||
© 版权所有 2021, Peking University.
|
||
|
||
</p>
|
||
</div>
|
||
|
||
|
||
|
||
利用 <a href="https://www.sphinx-doc.org/">Sphinx</a> 构建,使用了
|
||
|
||
<a href="https://github.com/readthedocs/sphinx_rtd_theme">主题</a>
|
||
|
||
由 <a href="https://readthedocs.org">Read the Docs</a>开发.
|
||
|
||
</footer>
|
||
</div>
|
||
</div>
|
||
|
||
</section>
|
||
|
||
</div>
|
||
|
||
|
||
<script type="text/javascript">
|
||
jQuery(function () {
|
||
SphinxRtdTheme.Navigation.enable(true);
|
||
});
|
||
</script>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
</body>
|
||
</html> |