arkcompiler_ets_runtime/docs/development-example-zh.md
wengchangcheng 0b947ac7d0 Descriptor: 部件化整改 ark -> arkcompiler
Details: 部件化整改 ark -> arkcompiler

Issue: https://gitee.com/openharmony/arkcompiler_ets_runtime/issues/I5VJMH

Signed-off-by: wengchangcheng <wengchangcheng@huawei.com>
Change-Id: I01c72bafc95a7d469c9bb31cfcbac79b310dfac1
2022-10-13 22:23:53 +08:00

13 KiB
Raw Blame History

开发实例

本章节将介绍基于方舟运行时的开发测试实例。

HelloWorld

运行前准备

  1. 编译方舟运行时,编译命令:

    编译x64版本

    ./build.sh --product-name hispark_taurus_standard --build-target ark_js_host_linux_tools_packages --build-target ark_ts2abc_build  # arm平台和host端运行工具
    

    编译arm64版本

    ./build.sh --product-name hispark_taurus_standard --gn-args use_musl=true --target-cpu arm64 --build-target ark_js_vm
    

    编译arm32版本:

    ./build.sh --product-name hispark_taurus_standard --build-target  ark_js_runtime
    
  2. 编译方舟前端,编译命令:

    ./build.sh --product-name hispark_taurus_standard --build-target ark_ts2abc_build
    

说明上述编译命令为release版本且执行路径为项目根目录。编译debug版本需增加编译选项--gn-args is_debug=true。

运行hello-world.js

新建hello-world.js文件写入以下源码

 print("Hello World!!!");

运行步骤:

  1. 通过方舟前端生成hello-world.abc文件编译命令

    node --expose-gc /your code path/out/hispark_taurus/clang_x64/arkcompiler/ets_frontend/build/src/index.js hello-world.js
    
  2. 执行hello-world.abc文件

    1. 设置搜索路径:

      export LD_LIBRARY_PATH= /your code path/out/hispark_taurus/clang_x64/arkcompiler/ets_runtime:/your code path/out/hispark_taurus/clang_x64/global/i18n_standard:/your code path/prebuilts/clang/ohos/linux-x86_64/llvm/lib
      
    2. 执行ark_js_vm

      /your code path/out/hispark_taurus/clang_x64/arkcompiler/ets_runtime/ark_js_vm hello-world.abc
      

      执行结果如下:

      Hello World!!!
      

说明:此处“your code path”为源码目录路径。

反汇编hello-world.abc

编译生成反汇编工具:

./build.sh --product-name hispark_taurus_standard --build-target ark_host_linux_tools_packages

执行如下命令结果输出到output.pa文件中

./your code path/out/hispark_taurus/clang_x64/arkcompiler/runtime_core/ark_disasm hello-world.abc output.pa

hello-world.abc反汇编结果如下

#
# source binary: hello-world.abc
#

# ====================
# LITERALS

# ====================
# RECORDS

.record _ESAnnotation <external>

.record _ESModuleMode {
	u8 isModule
}

# ====================
# METHODS

.function any func_main_0_any_any_any_any_(any a0, any a1, any a2) <static> {
	mov v2, a2
	mov v1, a1
	mov v0, a0
	builtin.acc
	sta v5
	builtin.idi "print", 0x0 // 加载print函数
	sta v3
	lda.str "Hello World!!!"  // 加载Hello World!!!字符串
	sta v4
	builtin.tern3 v3, v4  // 调用print函数
	builtin.acc
}

运行Test262测试用例

运行前准备

  1. 编译方舟运行时,编译命令:
./build.sh --product-name hispark_taurus_standard --build-target ark_js_host_linux_tools_packages
  1. 编译方舟前端,编译命令:
./build.sh --product-name hispark_taurus_standard --build-target ark_ts2abc_build

说明:编译命令执行路径为项目根目录。

运行Test262

运行run_test262.py脚本下载及运行Test262用例。

命令行格式:

python3 test262/run_test262.py [options]

执行路径为:项目根目录/arkcompiler/ets_frontend。

选项

描述

--h--help

帮助提示

--dir DIR

选定要测试的目录

--file FILE

选定要测试的文件

--mode [{1, 2, 3}]

模式选择1仅默认值2仅严格模式3默认模式和严格模式

--es51

运行Test262 ES5.1版本

--es2015 [{all, only}]

运行Test262 ES2015版本all包含的所有用例only仅包括ES2015

--esnext

运行Test262-ES.next

--engine FILE

运行测试的其他引擎,指定二进制文件(如:d8,hermes,jsc,qjs...

--babel

是否使用Babel转换

--timeout TIMEOUT

设置测试超时时间(以毫秒为单位)

--threads THREADS

设置并行运行线程数

--hostArgs HOSTARGS

传递给eshost主机的命令行参数

--ark-tool ARK_TOOL

方舟运行时的二进制工具

--ark-frontend-tool ARK_FRONTEND_TOOL

方舟前端转换工具

--libs-dir LIBS_DIR

依赖so的路径集合通过“:”分割

--ark-frontend [{ts2panda, es2panda}]

指定前端

测试运行示例

  • 运行ES51测试用例

     python3 test262/run_test262.py --es51
    
  • 仅运行ES2015测试用例

     python3 test262/run_test262.py --es2015
    
  • 仅运行ES2021测试用例

     python3 test262/run_test262.py --es2021 only
    
    
  • 运行ES2015和ES51和ES2021所有测试用例

     python3 test262/run_test262.py --es2021 all
    
  • 运行单一测试用例:

     python3 test262/run_test262.py --file test262/data/test_es5/language/statements/break/12.8-1.js
    
  • 运行某目录下所有测试用例:

     python3 test262/run_test262.py --dir test262/data/test_es5/language/statements
    
  • 使用`babel`把单个测试用例转换成es5后再运行

     python3 test262/run_test262.py  --babel --file test262/data/test_es5/language/statements/break/12.8-1.js
    

测试输出

Test262所有用例的测试结果位于项目根目录/arkcompiler/ets_frontend/out下。shell中测试输出结果如下

$python3 test262/run_test262.py --file test262/data/test_es2015/built-ins/Array/15.4.5.1-5-1.js

Wait a moment..........
Test command:
node
        test262/harness/bin/run.js
        --hostType=panda
        --hostPath=python3
        --hostArgs='-B test262/run_sunspider.py --ark-tool=/your code path/out/hispark_taurus/clang_x64/arkcompiler/ets_runtime/ark_js_vm --ark-frontend-tool=/your code path/out/hispark_taurus/clang_x64/arkcompiler/ets_frontend/build/src/index.js --libs-dir=/your code path/out/hispark_taurus/clang_x64/global/i18n:/your code path/prebuilts/clang/ohos/linux-x86_64/llvm/lib/ --ark-frontend=ts2panda'
        --threads=15
        --mode=only strict mode
        --timeout=60000
        --tempDir=build/test262
        --test262Dir=test262/data
        --saveCompiledTests
        test262/data/test_es5/language/statements/break/12.8-1.js

PASS test262/data/test_es2015/built-ins/Array/15.4.5.1-5-1.js (strict mode)
Ran 1 tests
1 passed
0 failed
used time is: 0:01:04.439642