zero copy blob
This commit is contained in:
@@ -78,15 +78,13 @@ JSC_CCALL(file_read,
|
||||
if (!pd_file) return JS_RaiseDisrupt(js, "file not initialized");
|
||||
SDFile *f = js2sdfile(js, argv[0]);
|
||||
unsigned int len = (unsigned int)js2number(js, argv[1]);
|
||||
void *buf = malloc(len);
|
||||
if (!buf) return JS_RaiseDisrupt(js, "malloc failed");
|
||||
int read = pd_file->read(f, buf, len);
|
||||
if (read < 0) {
|
||||
free(buf);
|
||||
void *out;
|
||||
JSValue blob = js_new_blob_alloc(js, len, &out);
|
||||
if (JS_IsException(blob)) return blob;
|
||||
int bytes_read = pd_file->read(f, out, len);
|
||||
if (bytes_read < 0)
|
||||
return JS_NULL;
|
||||
}
|
||||
JSValue blob = js_new_blob_stoned_copy(js, buf, read);
|
||||
free(buf);
|
||||
js_blob_stone(blob, bytes_read);
|
||||
return blob;
|
||||
)
|
||||
|
||||
|
||||
@@ -139,15 +139,13 @@ JSC_CCALL(http_read,
|
||||
if (!pd_network || !pd_network->http) return JS_RaiseDisrupt(js, "network not initialized");
|
||||
HTTPConnection *conn = js2http(js, argv[0]);
|
||||
unsigned int buflen = (unsigned int)js2number(js, argv[1]);
|
||||
void *buf = malloc(buflen);
|
||||
if (!buf) return JS_RaiseDisrupt(js, "malloc failed");
|
||||
int read = pd_network->http->read(conn, buf, buflen);
|
||||
if (read < 0) {
|
||||
free(buf);
|
||||
void *out;
|
||||
JSValue blob = js_new_blob_alloc(js, buflen, &out);
|
||||
if (JS_IsException(blob)) return blob;
|
||||
int bytes_read = pd_network->http->read(conn, out, buflen);
|
||||
if (bytes_read < 0)
|
||||
return JS_NULL;
|
||||
}
|
||||
JSValue blob = js_new_blob_stoned_copy(js, buf, read);
|
||||
free(buf);
|
||||
js_blob_stone(blob, bytes_read);
|
||||
return blob;
|
||||
)
|
||||
|
||||
@@ -218,15 +216,13 @@ JSC_CCALL(tcp_read,
|
||||
if (!pd_network || !pd_network->tcp) return JS_RaiseDisrupt(js, "network not initialized");
|
||||
TCPConnection *conn = js2tcp(js, argv[0]);
|
||||
size_t len = (size_t)js2number(js, argv[1]);
|
||||
void *buf = malloc(len);
|
||||
if (!buf) return JS_RaiseDisrupt(js, "malloc failed");
|
||||
int read = pd_network->tcp->read(conn, buf, len);
|
||||
if (read < 0) {
|
||||
free(buf);
|
||||
return JS_NewInt32(js, read); // Return error code
|
||||
}
|
||||
JSValue blob = js_new_blob_stoned_copy(js, buf, read);
|
||||
free(buf);
|
||||
void *out;
|
||||
JSValue blob = js_new_blob_alloc(js, len, &out);
|
||||
if (JS_IsException(blob)) return blob;
|
||||
int bytes_read = pd_network->tcp->read(conn, out, len);
|
||||
if (bytes_read < 0)
|
||||
return JS_NewInt32(js, bytes_read); // Return error code
|
||||
js_blob_stone(blob, bytes_read);
|
||||
return blob;
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user