質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.47%
TypeScript

TypeScriptは、マイクロソフトによって開発された フリーでオープンソースのプログラミング言語です。 TypeScriptは、JavaScriptの構文の拡張であるので、既存の JavaScriptのコードにわずかな修正を加えれば動作します。

Q&A

解決済

1回答

604閲覧

AssemblyScriptで作成したWASMから関数を呼び出したい。

kuniatsu

総合スコア141

TypeScript

TypeScriptは、マイクロソフトによって開発された フリーでオープンソースのプログラミング言語です。 TypeScriptは、JavaScriptの構文の拡張であるので、既存の JavaScriptのコードにわずかな修正を加えれば動作します。

0グッド

0クリップ

投稿2019/09/15 14:03

typeScript

1export function test(): string{ 2 return "test_test"; 3}

このTSを --noRuntime でWASMにコンパイルして読みだしたいのですが、
test_test ではなく 32という値が返されます。
どのように変更すれば test_test が返されますか?

index.html

HTML

1<!DOCTYPE html> 2<html> 3 <head> 4 <meta charset="utf-8"> 5 <title>test template</title> 6 </head> 7 <body> 8 <script> 9 WebAssembly.instantiateStreaming(fetch("test.wasm"), {}).then(mod => { 10 const test = mod.instance.exports.test; 11 const result = test(); 12 console.log(result); 13 }); 14 </script> 15 </body> 16</html>

test.wast

WAST

1(module 2 (type $i (func (result i32))) 3 (memory $0 1) 4 (data (i32.const 32) "\t\00\00\00\t\00\00\00t\00e\00s\00t\00_\00t\00e\00s\00t\00") 5 (export "test" (func $test)) 6 (export "memory" (memory $0)) 7 (func $test (type $i) (result i32) 8 (return 9 (i32.const 32) 10 ) 11 ) 12)

コンパイル時のコマンド

tarminal

1$ asc --noRuntime -o test.wasm test.ts

環境やコードはこちらを参考にしました。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

以下、WebAssemblyどころかジャバスクもほとんど分からない人間の回答であることに留意ください。

まずはHow can I return a JavaScript string from a WebAssembly functionに書かれている通り、WebAssemblyからそのまま文字列を返すことはできません。WebAssemblyから返される生のバッファ領域(バイナリ)から文字列を生成する必要があります。
具体的にはTypeScriptからWebAssemblyを生成するAssemblyScriptを試すが参考になります。

ということで以下のようなコードで当方環境のChrome77.0.3865.75で文字列として扱えるようになりました。ただしコード中のmagic_numberを入れる理由は分かりません。

test.ts

TypeScript

1export function test(): string{ 2 return "test_テスト_てすと"; 3} 4export function test_len(): int{ 5 return test().length 6}

test.wat

※ascバージョン0.3.0にて変換

PlainText

1(module 2 (type (;0;) (func (result i32))) 3 (func $test (type 0) (result i32) 4 block ;; label = @1 5 i32.const 32 6 return 7 unreachable 8 end 9 unreachable) 10 (func $test_len (type 0) (result i32) 11 block ;; label = @1 12 call $test 13 i32.load offset=4 14 return 15 unreachable 16 end 17 unreachable) 18 (memory (;0;) 1) 19 (export "test" (func $test)) 20 (export "test_len" (func $test_len)) 21 (export "memory" (memory 0)) 22 (data (i32.const 32) "\0c\00\00\00\0c\00\00\00t\00e\00s\00t\00_\00\c60\b90\c80_\00f0Y0h0"))

index.html

html

1<!DOCTYPE html> 2<html> 3 <head> 4 <meta charset="utf-8"> 5 <title>test template</title> 6 </head> 7 <body> 8 <script> fetch('test.wasm').then(response => response.arrayBuffer() ).then(bytes => 9 WebAssembly.instantiate(bytes) ).then(obj => { 10 const buffer = obj.instance.exports.memory.buffer; 11 const offset = obj.instance.exports.test(); 12 const test_len = obj.instance.exports.test_len(); 13 console.log( buffer); 14 console.log( offset); 15 console.log( test_len); 16 const magic_number = 8; // よく分からない 17 const array = new Uint16Array( buffer, offset+magic_number, test_len); 18 const str = new TextDecoder('utf-16').decode(array); 19 console.log( '['+str+']' ); 20 }); 21 </script> 22 </body> 23</html>

結果

イメージ説明

投稿2019/09/16 03:18

can110

総合スコア38266

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

kuniatsu

2019/09/17 00:00

ありがとうございます!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.47%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問