直接上码了………………

.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 )
(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
i32.shl
i32.add
tee_local $l1
i32.load
set_local $l4
get_local $p0
get_local $l0
i32.const
i32.add
tee_local $l6
i32.const
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
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 -
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 -
i32.add
tee_local $p2
i32.lt_s
br_if $L6
br $B2
end
end
get_local $p0
get_local $l6
i32.const
i32.add
tee_local $l6
i32.const
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
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
i32.const
i32.const
i32.const
call $mainSrot
i32.const
i32.const
call $env.returnArr)
(table $T0 anyfunc)
(memory $memory )
(export "memory" (memory ))
(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+;
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,,N-);
//调用JavaScript中的函数
returnArr(array,N);
}

html和js源码:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style> </style>
</head>
<body>
<div class="box">
<label>请输入排序前的数组:</label>
<input type="text" class="beforeArr">
<button class="btn">点击排序</button>
</div>
<script> //继承自HTMLElement
class sortArray extends HTMLElement{
constructor(){
super();
let textBore = document.querySelector(".beforeArr");
//事件添加
document.querySelector('.btn').onclick = ()=>{
let beforeArr = this.strArr(textBore.value);
//调用webassembly
arrSort(beforeArr); }
//调用影子dom 添加自定义html模板
let shadow = this.attachShadow({mode:'open'});
let vnode = this.nodeTowfragment('.box');
shadow.appendChild(vnode);
}
//把dom放入内存中
nodeTowfragment(str){
let el = document.querySelector(str);
let fragment = document.createDocumentFragment();
let firstChild;
while(firstChild = el.firstChild){
fragment.appendChild(firstChild);
}
return fragment;
}
//便利push确保是数字
strArr(el){
          let arr = [];
for(let i = ; i < el.length; i++){
arr.push(parseInt(el[i]));
}
return arr; }
}
//实例化
customElements.define('sort-array',sortArray);
</script>
<sort-array></sort-array>
<script> function arrSort(arr){
/**
调用浏览器网络下载访问
编译后的wasm文件
*/
fetch('program.wasm')
//转换为arrayBffer也就是二进制数据
.then(response => response.arrayBuffer())
.then((bytes) => {
let memory;
//通过浏览器的WebAssembly接口编译初始化wasm模块
WebAssembly.compile(bytes)
.then(module => WebAssembly.instantiate(module,{ env:{
/**
JavaScript中实现c中调用的函数
能取到排序后的数组
*/
returnArr(offset,len){
let arrBuffer = new Uint32Array(memory.buffer,offset,len);
//转换为数组
let arr = Array.from(arrBuffer);
alert('排序完成:'+arr) ; }
}
}))
.then(instance => {
//取出wasm中导出的c函数
let exports = instance.exports;
//获得wasm模块中的实例堆内存对象
memory = exports.memory;
/**
调用从JavaScript环境向指定的c数组地址填充数据的方法并且传入参数
@param memory //要操作的内存
@param beforeArr //要操作的数组
@param arrOffset //定义c中数组的长度与初始化数组,并且返回该数组的首地址 */
importArrayToBuffer(memory,arr,exports.getArrayOffset());
/**
调用排序方法
@param len //数组的长度 必须与wasm模块导出的getArrayOffset传入的值一样
*/
exports.sort();
})
}) } /**
该方法用于从JavaScript环境向指定的c数组地址填充数据
*/
function importArrayToBuffer(memory,array,offset){
const importBuffer = new Uint32Array(memory.buffer,offset,array.length);
for(let n = ; n < array.length; n++){
importBuffer[n] = array[n];
}
}
</script>
</body>
</html>

输入:

输出:

结束……

webAssmebly实现js数组排序 使用custom elements和Shadow DOM自定义组件的更多相关文章

  1. 使用custom elements和Shadow DOM自定义标签

    具体的api我就不写 官网上面多  如果不知道这个的话也可以搜索一下 目前感觉这个还是相当好用的直接上码. <!DOCTYPE html> <html lang="en&q ...

  2. Web Components之Custom Elements

    什么是Web Component? Web Components 包含了多种不同的技术.你可以把Web Components当做是用一系列的Web技术创建的.可重用的用户界面组件的统称.Web Com ...

  3. [HTML5] Render Hello World Text with Custom Elements

    Custom elements are fun technology. In this video, you will learn how to set one up and running in l ...

  4. JS数组排序技巧汇总(冒泡、sort、快速、希尔等排序)

    本文实例总结了JS数组排序技巧.分享给大家供大家参考,具体如下: 1.冒泡排序 var temp = 0; for (var i = 0; i < array.length; i++) { fo ...

  5. window 属性:自定义元素(custom elements)

      概述 Web Components 标准非常重要的一个特性是,它使开发者能够将HTML页面的功能封装为 custom elements(自定义标签),而往常,开发者不得不写一大堆冗长.深层嵌套的标 ...

  6. js数组排序,支持正反排序以及多维度排序

    工作中遇到js数组排序问题,数组中存储的都是对象,于是就百度了下,利用别人的代码进行修改,最终完成可以倒序.反序,可以进行多维度排序的功能源码如下: /** * js数组排序 支持数字和字符串 * @ ...

  7. js数组排序 reverse()和sort()方法的使用

    WEB前端|js数组排序reverse()和sort()方法的使用,数组中已经存在两个可以直接用来重排序的方法:reverse()和sort(). reverse()方法会对反转数组项的顺序. var ...

  8. 自定义元素(custom elements)

    记录下自定义html自定义元素的相关心得: 浏览器将自定义元素保留在 DOM 之中,但不会任何语义.除此之外,自定义元素与标准元素都一致 事实上,浏览器提供了一个HTMLUnknownElement, ...

  9. HTML Custom Elements & valid name

    HTML Custom Elements & valid name valid custom element name https://html.spec.whatwg.org/multipa ...

随机推荐

  1. xml中运用js和jq

    1.点击事件参数为this 一般<a>标签中会使用href和onclick两种方式来进行进行页面跳转或执行动作,但是小编一般都会使用onclick来进行执行Ajax函数进行跳转,并同时使用 ...

  2. 后端开发福音!GitHub上15W+的后台控制面板!

    Web 开发中几乎的平台都需要一个后台管理,但是从零开发一套后台控制面板并不容易,幸运的是有很多开源免费的后台控制面板可以给开发者使用,那么有哪些优秀的开源免费的控制面板呢?我在 Github 上收集 ...

  3. shell学习(5)- sort

    Linux sort命令用于将文本文件内容加以排序. sort可针对文本文件的内容,以行为单位来排序. 参数如下: -b 忽略每行前面开始出的空格字符. -c 检查文件是否已经按照顺序排序. -d 排 ...

  4. Image.resize()和Image.thumbnail()的区别

    Image.resize()和Image.thumbnail()的区别 根据代码和代码注释, 这两个函数都是对图片进行缩放, 两者的主要区别如下: resize()函数会返回一个Image对象, th ...

  5. python 基础(十二) 图片简单处理

    pillow 图片处理模块 安装 pip install pillow  pip是安装第三方模块的工具 缩放图片实例 from PIL import Image path = r'C:\Users\x ...

  6. bzoj 2441 [中山市选2011]小W的问题

    bzoj 2441 [中山市选2011]小W的问题 Description 有一天,小W找了一个笛卡尔坐标系,并在上面选取了N个整点.他发现通过这些整点能够画出很多个"W"出来.具 ...

  7. HDU6441(费马大定理)

    听队友说过结论:a^n + b^n = c^n在n > 2时无解. 勾股那里本菜数学不好直接暴举了Orz. 跟大家学一波勾股数的构造:a是奇数时,tmp = a / 2; b = (tmp + ...

  8. Ubuntu设置右键打开终端

    1:设置Ubuntu右键打开终端. Ctrl+Alt+T 打开终端 $ sudo apt-get ins tall nautilus-open-terminal 重启系统 2:进入root用户认证失败 ...

  9. Leetcode 题解 - 目录

    本文从 Leetcode 中精选大概 200 左右的题目,去除了某些繁杂但是没有多少算法思想的题目,同时保留了面试中经常被问到的经典题目. 算法思想 双指针 排序 贪心思想 二分查找 分治 搜索 动态 ...

  10. shell脚本由基础变量及特殊变量($@、$*、$#等)到实战。

    一.shell脚本建立: shell脚本通常是在编辑器(如vi/vim)中编写,也可以在命令行中直接执行: 1.脚本开头:     规范的脚本第一行需要指出有哪个程序(解释器)来执行脚本中的内容,在L ...