直接上码了………………
.wat源码
(module (type $t0 (func (param i32 i32))) (type $t1 (func (result i32))) (type $t2 (func (param i32 i32 i32 i32))) (type $t3 (func)) (import "env" "returnArr" (func $env.returnArr (type $t0))) (func $getArrayOffset (type $t1) (result i32) i32.const 16) (func $swap (type $t0) (param $p0 i32) (param $p1 i32) (local $l0 i32) get_local $p0 i32.load set_local $l0 get_local $p0 get_local $p1 i32.load i32.store get_local $p1 get_local $l0 i32.store) (func $mainSrot (type $t2) (param $p0 i32) (param $p1 i32) (param $p2 i32) (param $p3 i32) (local $l0 i32) (local $l1 i32) (local $l2 i32) (local $l3 i32) (local $l4 i32) (local $l5 i32) (local $l6 i32) block $B0 get_local $p2 get_local $p3 i32.ge_s br_if $B0 loop $L1 get_local $p0 get_local $p2 tee_local $l0 i32.const 2 i32.shl i32.add tee_local $l1 i32.load set_local $l4 get_local $p0 get_local $l0 i32.const 1 i32.add tee_local $l6 i32.const 2 i32.shl i32.add tee_local $l2 i32.load set_local $l5 block $B2 block $B3 get_local $l6 get_local $p3 i32.ge_s br_if $B3 get_local $p3 set_local $p2 loop $L4 get_local $p0 get_local $p2 i32.const 2 i32.shl i32.add set_local $l3 block $B5 loop $L6 get_local $l5 get_local $l4 i32.le_s br_if $B5 get_local $l2 get_local $l3 i32.load i32.store get_local $l3 get_local $l5 i32.store get_local $l3 i32.const -4 i32.add set_local $l3 get_local $l1 i32.load set_local $l4 get_local $l2 i32.load set_local $l5 get_local $l6 get_local $p2 i32.const -1 i32.add tee_local $p2 i32.lt_s br_if $L6 br $B2 end end get_local $p0 get_local $l6 i32.const 1 i32.add tee_local $l6 i32.const 2 i32.shl i32.add tee_local $l2 i32.load set_local $l5 get_local $l6 get_local $p2 i32.lt_s br_if $L4 br $B2 end end get_local $p3 set_local $p2 end get_local $l1 get_local $p0 get_local $l6 get_local $l5 get_local $l4 i32.ge_s i32.sub tee_local $l3 i32.const 2 i32.shl i32.add tee_local $l5 i32.load i32.store get_local $l5 get_local $l4 i32.store get_local $p0 get_local $p1 get_local $l0 get_local $l3 call $mainSrot get_local $p2 get_local $p3 i32.lt_s br_if $L1 end end) (func $sort (type $t3) i32.const 16 i32.const 20 i32.const 0 i32.const 19 call $mainSrot i32.const 16 i32.const 20 call $env.returnArr) (table $T0 0 anyfunc) (memory $memory 1) (export "memory" (memory 0)) (export "getArrayOffset" (func $getArrayOffset)) (export "swap" (func $swap)) (export "mainSrot" (func $mainSrot)) (export "sort" (func $sort)))
c源码:
#define N 20//外部导入javascript函数extern void returnArr(int * offset, int length);int array[N];//定义数组并且取到数组首地址int * getArrayOffset(){ return array;}//交换2个数的值void swap(int *a , int *b){ int temp; temp = *a; *a = *b; *b = temp;}// 主排序方法void mainSrot(int array[], int maxlen, int begin, int end){ int i,j; if(begin < end){ i = begin+1; j = end; while (i < j){ if(array[i] > array[begin]){ swap(&array[i],&array[j]); j--; }else{ i++; } } if(array[i] >= array[begin]){ i--; } swap(&array[begin],&array[i]); mainSrot(array,maxlen,begin,i); mainSrot(array,maxlen,j,end); }}//JavaScript调用方法void sort(){ mainSrot(array,N,0,N-1); //调用JavaScript中的函数 returnArr(array,N);}
html和js源码:
Document
输入:
输出:
结束……