我正在学习 Blazor 技术。我在 VS 2019 中启动了一个默认的增量项目,并且我已经使用 confirm() 和 alert 修改了 Decrement 的代码,但它不起作用。
@page "/counter"
<h1>Counter</h1>
<p>Current count: @currentCount</p>
<button class="btn btn-primary" @onclick="IncrementCount">Increment</button>
<button class="btn btn-primary btn-danger" onclick="if (confirm('Are you sure to Decrement')) { @DecrementCount() }">Decrement</button>
@code {
private int currentCount = 0;
private void IncrementCount()
{
currentCount++;
}
private void DecrementCount()
{
currentCount--;
// alert('Operation Successfully executed')
}
}
Run Code Online (Sandbox Code Playgroud)
在我的代码片段中,confirm() 函数运行良好,但我想调用 Decrement 函数不起作用构建失败。我想在我的函数中添加一条成功消息。请提供任何选项,而不是使用 confirm(),alert() 函数。
我按照Webassembly入门教程http://webassembly.org/getting-started/developers-guide/
它运作良好,并显示"你好,世界!" 浏览器中的消息.
然后我尝试了一个小的C++代码,打开一个文本文件并在读取文件后进行计算(10*20).
emcc编译文件就好了,没有错误.
但是当我通过运行emrun通过HTTP提供文件时,它无法打开文件.
这是我在emrun Web控制台中看到的:
Unable to open file
200
Run Code Online (Sandbox Code Playgroud)
从本地磁盘打开文件有什么限制吗?
[thiago@terra hello]$ cat pfile.cpp
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
string line;
int a, b, c;
ifstream myfile("test.txt");
if (myfile.is_open()) {
while (getline (myfile, line)) {
cout << line << endl;
}
myfile.close();
}
else cout << "Unable to open file" << endl;
a = 10;
b = 20;
c = a * b;
cout << c << endl; …
Run Code Online (Sandbox Code Playgroud) 首先,我已经阅读了为什么我的WebAssembly功能比JavaScript等效的慢?
但它对这个问题没有什么启示,而且我投入了大量的时间,很可能是那些黄色的东西.
我不使用全局变量,我不使用任何内存.我有两个简单的函数,可以找到一个线段的长度,并将它们与普通的旧Javascript中的相同内容进行比较.我有4个参数3个本地人并返回一个浮点数或双倍.
在Chrome上,Javascript比webAssembly快40倍,而在Firefox上,wasm 比Javascript慢近300倍.
我在jsPref WebAssembly V Javascript数学中添加了一个测试用例
或
请选择1.
我已经阅读了webAssembly用例
通过定位WebAssembly重用现有代码,嵌入在更大的JavaScript/HTML应用程序中.这可以是简单的帮助程序库,也可以是面向计算的任务卸载.
我希望我可以用webAssembly替换一些几何库来获得一些额外的性能.我希望它会很棒,比10倍或更快.但是WTF要慢300倍.
这不是JS优化问题.
为了确保优化具有尽可能小的效果,我使用以下方法测试以减少或消除任何优化偏差.
c += length(...
以确保执行所有代码.bigCount += c
确保执行整个功能.不需要Math.hypot
用于证明代码正在运行.// setup and associated functions
const setOf = (count, callback) => {var a = [],i = 0; while (i < count) { a.push(callback(i ++)) } return a };
const rand = (min = 1, max = …
Run Code Online (Sandbox Code Playgroud)我想测试WebAssembly做一些复杂的数组计算.
所以我写了一个简单的C++函数,添加了两个int
包含3个元素的数组:
// hello.cpp
extern "C" {
void array_add(int * summed, int* a, int* b) {
for (int i=0; i < 3; i++) {
summed[i] = a[i] + b[i];
}
}
}
Run Code Online (Sandbox Code Playgroud)
并编译了这个:
emcc hello.cpp -s WASM=1 -s "MODULARIZE=1" -s "EXPORT_NAME='HELLO'" -s "BINARYEN_METHOD='native-wasm'" -s "EXPORTED_FUNCTIONS=['_array_add']" -o build/hello.js
其中包括a js
和a wasm
文件.我用以下html页面加载这些:
<!doctype html>
<html lang="en-us">
<head>
<meta charset="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript" src="build/hello.js"></script>
<script type="text/javascript">
function reqListener () {
// Loading wasm module
var arrayBuffer …
Run Code Online (Sandbox Code Playgroud) 我正在尝试在WebAssembly中实现JWT令牌(仅编码),目标是拥有一个非常轻量级的 wasm模块.作为Web开发人员,我的C知识是有限的.现在我已经实现了以下功能(从JS移植)来编码url-safe Base64编码器,它完美地工作.
char _keyStr[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_=";
char ret_val[200];
char* encode (char *data){
int len = strlen(data);
int i = 0;
int j = 0;
while(i<len){
char chr1 = data[i++];
int chr2Out = (i > len - 1)? 1:0;
char chr2 = data[i++];
int chr3Out = (i > len - 1)? 1:0;;
char chr3 = data[i++];
char enc1 = chr1 >> 2;
char enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
char enc3 = ((chr2 & …
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用Chrome上的fetch api加载.wasm文件,并使用express提供html文件.但chrome不允许我加载文件:
'未捕获(承诺)TypeError:无法在'WebAssembly'上执行'compile':错误的响应MIME类型.预计'申请/ wasm'.'
这是我的文件:
/public/index.html
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<script type="text/javascript">
WebAssembly.instantiateStreaming(fetch('http://localhost:3000/simple.wasm'))
.then(obj => {
console.log(obj.instance.exports.add(1, 2)); // "3"
});
</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
快递服务:
/index.js
const express = require('express')
express.static.mime.define({'application/wasm': ['wasm']})
var app = express();
app.use('/', express.static('public'));
app.listen(3000, function () {
console.log('Example app listening on port 3000!')
})
Run Code Online (Sandbox Code Playgroud)
我可以在提供.wasm文件时添加新的mime类型来表达吗?或允许铬接受它?我对如何解决它没有任何想法^^
请参阅:http://kripken.github.io/emscripten-site/docs/compiling/WebAssembly.html
Web server setup
To serve wasm in the most efficient way over the network, make sure your web server …
Run Code Online (Sandbox Code Playgroud) 当我cargo install wasm-pack
在 Windows 10 64 位上运行时,出现此错误:
error: failed to run custom build command for `openssl-sys v0.9.65`\n\nCaused by:\n process didn't exit successfully: `C:\\Users\\vilgo\\AppData\\Local\\Temp\\cargo-install2J8ZNz\\release\\build\\openssl-sys-932395a164949059\\build-script-main` (exit code: 101)\n --- stdout\n cargo:rustc-cfg=const_fn\n cargo:rerun-if-env-changed=X86_64_PC_WINDOWS_MSVC_OPENSSL_NO_VENDOR\n X86_64_PC_WINDOWS_MSVC_OPENSSL_NO_VENDOR unset\n cargo:rerun-if-env-changed=OPENSSL_NO_VENDOR\n OPENSSL_NO_VENDOR unset\n openssl-src: Enable the assembly language routines in building OpenSSL.\n running "perl" "./Configure" "--prefix=C:\\\\Users\\\\vilgo\\\\AppData\\\\Local\\\\Temp\\\\cargo-install2J8ZNz\\\\release\\\\build\\\\openssl-sys-a51d272dcebf1fc5\\\\out\\\\openssl-build\\\\install" "no-dso" "no-shared" "no-ssl3" "no-unit-test" "no-comp" "no-zlib" "no-zlib-dynamic" "no-md2" "no-rc5" "no-weak-ssl-ciphers" "no-camellia" "no-idea" "no-seed" "no-engine" "VC-WIN64A"\n\n --- stderr\n thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Os { …
Run Code Online (Sandbox Code Playgroud) 有没有办法在没有JavaScript的情况下获得对DOM和/或WebAPI(即全屏API)的读/写访问权限?
我正在尝试用C构建一个基本的应用程序(C源实际上是从GC语言转换的结果).我正在构建的应用程序将作为桌面应用程序运行(它不打算在"真正的"浏览器中运行)所以我可以根据需要调整环境(即布局引擎).
我一直在尝试加载一个 WebAssembly (.wasm) 文件——生成的 C++ 代码由 Emscripten 编译为 WebAssembly——在 React-Native 应用程序中。
这是我获取 .wasm 文件的代码:
import fs from 'react-native-fs';
if (!global.WebAssembly) {
global.WebAssembly = require('webassemblyjs');
}
const fetchWasm = async () => {
const wasm = await fetch(`${fs.MainBundlePath}/calculator.wasm`);
console.log(wasm);
// Returns: Response {type: "default", status: 200, ok: true, statusText: undefined, headers: Headers, …}
const mod = await WebAssembly.instantiateStreaming(wasm);
console.log(mod);
// Throws: TypeError: Failed to execute 'compile' on 'WebAssembly': An argument must be provided, which must be a Response or Promise<Response> object …
Run Code Online (Sandbox Code Playgroud) 我目前正在开发一个 .NET Standard 2.1 Blazor WebAssembly 应用程序。我尝试根据环境变量包含或排除样式表。
在 .NET Core 中,通常有环境标签助手,如下例所示:
<environment include="Development">
<link rel="stylesheet" href="css/style.css" type="text/css" />
</environment>
<environment exclude="Development">
<link rel="stylesheet" href="css/style.min.css" type="text/css" />
</environment>
Run Code Online (Sandbox Code Playgroud)
这在 Blazor 服务器应用程序中工作得非常好,但在 Blazor WASm 中却没有,因为这是客户端代码。
因此,我试图找到一个很好的解决方案,根据Blazor WebAssembly 中的环境变量包含/排除样式表。
我目前的方法是使用 JSInterop 从我的 Blazor WASm Program.cs 文件中调用 JavaScript 辅助方法,并根据环境变量删除样式表:
await jsInterop.InvokeVoidAsync("helpers.setup", "Development");
Run Code Online (Sandbox Code Playgroud)
我在客户端的 JavaScript 如下所示:
window.helpers = {
setup: (environment) => {
if (environment === "Development") {
// remove production styles
}
if (environment !== "Development") {
// remove development …
Run Code Online (Sandbox Code Playgroud) webassembly ×10
javascript ×4
emscripten ×3
asp.net-core ×2
blazor ×2
c# ×2
c++ ×2
browser ×1
c ×1
express ×1
jwt ×1
openssl ×1
performance ×1
react-native ×1
rust ×1
rust-cargo ×1
sha256 ×1
wasm-pack ×1
webidl ×1