File tree Expand file tree Collapse file tree 4 files changed +26
-0
lines changed Expand file tree Collapse file tree 4 files changed +26
-0
lines changed Original file line number Diff line number Diff line change @@ -41,6 +41,7 @@ under the licensing terms detailed in LICENSE:
41
41
* Ryan Pivovar <ryanpivovar@gmail.com>
42
42
* Roman F. <70765447+romdotdog@users.noreply.github.com>
43
43
* Joe Pea <trusktr@gmail.com>
44
+ * Felipe Gasper <fgasper@users.noreply.github.com>
44
45
45
46
Portions of this software are derived from third-party works licensed under
46
47
the following terms:
Original file line number Diff line number Diff line change @@ -86,6 +86,8 @@ export interface ASUtil {
86
86
__instanceof ( ptr : number , baseId : number ) : boolean ;
87
87
/** Allocates a new string in the module's memory and returns a reference (pointer) to it. */
88
88
__newString ( str : string ) : number ;
89
+ /** Allocates a new ArrayBuffer in the module's memory and returns a reference (pointer) to it. */
90
+ __newArrayBuffer ( buf : ArrayBuffer ) : number ;
89
91
/** Allocates a new array in the module's memory and returns a reference (pointer) to it. */
90
92
__newArray ( id : number , values : ArrayLike < number > ) : number ;
91
93
Original file line number Diff line number Diff line change @@ -150,6 +150,18 @@ function postInstantiate(extendedExports, instance) {
150
150
151
151
extendedExports . __newString = __newString ;
152
152
153
+ /** Allocates a new ArrayBuffer in the module's memory and returns its pointer. */
154
+ function __newArrayBuffer ( buf ) {
155
+ if ( buf == null ) return 0 ;
156
+ const bufview = new Uint8Array ( buf ) ;
157
+ const ptr = __new ( bufview . length , ARRAYBUFFER_ID ) ;
158
+ const U8 = new Uint8Array ( memory . buffer ) ;
159
+ U8 . set ( bufview , ptr ) ;
160
+ return ptr ;
161
+ }
162
+
163
+ extendedExports . __newArrayBuffer = __newArrayBuffer ;
164
+
153
165
/** Reads a string from the module's memory by its pointer. */
154
166
function __getString ( ptr ) {
155
167
if ( ! ptr ) return null ;
Original file line number Diff line number Diff line change @@ -36,6 +36,17 @@ function test(file) {
36
36
assert . strictEqual ( exports . strlen ( ref ) , str . length ) ;
37
37
}
38
38
39
+ // should be able to allocate and work with a new small ArrayBuffer
40
+ {
41
+ let u8arr = new Uint8Array ( [ 1 , 2 , 3 , 4 ] ) ;
42
+ let buf = u8arr . buffer ;
43
+ let ref = exports . __newArrayBuffer ( buf ) ;
44
+ let got = exports . __getArrayBuffer ( ref ) ;
45
+ let gotu8arr = new Uint8Array ( got ) ;
46
+
47
+ assert . deepStrictEqual ( gotu8arr , u8arr ) ;
48
+ }
49
+
39
50
// should be able to allocate and work with a new big string
40
51
{
41
52
let str = `
You can’t perform that action at this time.
0 commit comments