22
33const {
44 ObjectFreeze,
5+ ObjectPrototypeToString,
56} = primordials ;
67const { Buffer } = require ( 'buffer' ) ;
78const { emitExperimentalWarning } = require ( 'internal/util' ) ;
9+ const {
10+ isArrayBufferView,
11+ } = require ( 'internal/util/types' ) ;
812const {
913 codes : {
1014 ERR_ACCESS_DENIED ,
@@ -32,6 +36,8 @@ const {
3236 getUint64,
3337 getFloat32,
3438 getFloat64,
39+ exportBytes,
40+ getRawPointer,
3541 setInt8,
3642 setUint8,
3743 setInt16,
@@ -114,21 +120,52 @@ function exportString(str, data, len, encoding = 'utf8') {
114120 targetBuffer . fill ( 0 , dataLength , dataLength + terminatorSize ) ;
115121}
116122
117- function exportBuffer ( buffer , data , len ) {
123+ function exportBuffer ( source , data , len ) {
118124 checkFFIPermission ( ) ;
119125
120- if ( ! Buffer . isBuffer ( buffer ) ) {
121- throw new ERR_INVALID_ARG_TYPE ( 'buffer' , 'Buffer' , buffer ) ;
126+ if ( ! Buffer . isBuffer ( source ) ) {
127+ throw new ERR_INVALID_ARG_TYPE ( 'buffer' , 'Buffer' , source ) ;
122128 }
123129
124130 validateInteger ( len , 'len' , 0 ) ;
125131
126- if ( len < buffer . length ) {
127- throw new ERR_OUT_OF_RANGE ( 'len' , `>= ${ buffer . length } ` , len ) ;
132+ if ( len < source . length ) {
133+ throw new ERR_OUT_OF_RANGE ( 'len' , `>= ${ source . length } ` , len ) ;
128134 }
129135
130- const targetBuffer = toBuffer ( data , len , false ) ;
131- buffer . copy ( targetBuffer , 0 , 0 , buffer . length ) ;
136+ exportBytes ( source , data , len ) ;
137+ }
138+
139+ function exportArrayBuffer ( source , data , len ) {
140+ checkFFIPermission ( ) ;
141+
142+ if ( ObjectPrototypeToString ( source ) !== '[object ArrayBuffer]' ) {
143+ throw new ERR_INVALID_ARG_TYPE ( 'arrayBuffer' , 'ArrayBuffer' , source ) ;
144+ }
145+
146+ validateInteger ( len , 'len' , 0 ) ;
147+
148+ if ( len < source . byteLength ) {
149+ throw new ERR_OUT_OF_RANGE ( 'len' , `>= ${ source . byteLength } ` , len ) ;
150+ }
151+
152+ exportBytes ( source , data , len ) ;
153+ }
154+
155+ function exportArrayBufferView ( source , data , len ) {
156+ checkFFIPermission ( ) ;
157+
158+ if ( ! isArrayBufferView ( source ) ) {
159+ throw new ERR_INVALID_ARG_TYPE ( 'arrayBufferView' , 'ArrayBufferView' , source ) ;
160+ }
161+
162+ validateInteger ( len , 'len' , 0 ) ;
163+
164+ if ( len < source . byteLength ) {
165+ throw new ERR_OUT_OF_RANGE ( 'len' , `>= ${ source . byteLength } ` , len ) ;
166+ }
167+
168+ exportBytes ( source , data , len ) ;
132169}
133170
134171const suffix = process . platform === 'win32' ? 'dll' : process . platform === 'darwin' ? 'dylib' : 'so' ;
@@ -163,6 +200,8 @@ module.exports = {
163200 dlopen,
164201 dlclose,
165202 dlsym,
203+ exportArrayBuffer,
204+ exportArrayBufferView,
166205 exportString,
167206 exportBuffer,
168207 getInt8,
@@ -175,6 +214,7 @@ module.exports = {
175214 getUint64,
176215 getFloat32,
177216 getFloat64,
217+ getRawPointer,
178218 setInt8,
179219 setUint8,
180220 setInt16,
0 commit comments