Making sure that are not malloc empty string or image

This commit is contained in:
Maicon Santana 2026-03-03 00:29:21 +00:00
parent 9b78594354
commit 5f8528b324
2 changed files with 48 additions and 37 deletions

View File

@ -840,28 +840,34 @@ EM_ASYNC_JS(void, RequestClipboardData, (void), {
EM_JS(char*, GetLastPastedText, (void), {
var str = window._lastClipboardString || "";
var length = lengthBytesUTF8(str) + 1;
var ptr = _malloc(length);
stringToUTF8(str, ptr, length);
return ptr;
if (length > 1) {
var ptr = _malloc(length);
stringToUTF8(str, ptr, length);
return ptr;
}
return 0;
});
// Returns the image created by RequestClipboardData from JS memory to Emscripten C memory
EM_JS(unsigned char*, GetLastPastedImage, (int* width, int* height), {
if (!window._lastImgData) return 0;
if (window._lastImgData) {
const data = window._lastImgData;
if (data.length > 0) {
const ptr = _malloc(data.length);
HEAPU8.set(data, ptr);
const data = window._lastImgData;
const ptr = _malloc(data.length);
HEAPU8.set(data, ptr);
// Set the width and height via the pointers passed from C
// HEAP32 handles the 4-byte integers
if (width) setValue(width, window._lastImgWidth, 'i32');
if (height) setValue(height, window._lastImgHeight, 'i32');
// Set the width and height via the pointers passed from C
// HEAP32 handles the 4-byte integers
if (width) setValue(width, window._lastImgWidth, 'i32');
if (height) setValue(height, window._lastImgHeight, 'i32');
// Clear the JS buffer so we don't fetch the same image twice
window._lastImgData = null;
// Clear the JS buffer so we don't fetch the same image twice
window._lastImgData = null;
return ptr;
return ptr;
}
}
return 0;
});
// Get clipboard text content

View File

@ -818,28 +818,34 @@ EM_ASYNC_JS(void, RequestClipboardData, (void), {
EM_JS(char*, GetLastPastedText, (void), {
var str = window._lastClipboardString || "";
var length = lengthBytesUTF8(str) + 1;
var ptr = _malloc(length);
stringToUTF8(str, ptr, length);
return ptr;
if (length > 1) {
var ptr = _malloc(length);
stringToUTF8(str, ptr, length);
return ptr;
}
return 0;
});
// Returns the image created by RequestClipboardData from JS memory to Emscripten C memory
EM_JS(unsigned char*, GetLastPastedImage, (int* width, int* height), {
if (!window._lastImgData) return 0;
if (window._lastImgData) {
const data = window._lastImgData;
if (data.length > 0) {
const ptr = _malloc(data.length);
HEAPU8.set(data, ptr);
const data = window._lastImgData;
const ptr = _malloc(data.length);
HEAPU8.set(data, ptr);
// Set the width and height via the pointers passed from C
// HEAP32 handles the 4-byte integers
if (width) setValue(width, window._lastImgWidth, 'i32');
if (height) setValue(height, window._lastImgHeight, 'i32');
// Set the width and height via the pointers passed from C
// HEAP32 handles the 4-byte integers
if (width) setValue(width, window._lastImgWidth, 'i32');
if (height) setValue(height, window._lastImgHeight, 'i32');
// Clear the JS buffer so we don't fetch the same image twice
window._lastImgData = null;
// Clear the JS buffer so we don't fetch the same image twice
window._lastImgData = null;
return ptr;
return ptr;
}
}
return 0;
});
// Get clipboard text content
@ -868,7 +874,6 @@ Image GetClipboardImage(void)
return image;
}
// Show mouse cursor
void ShowCursor(void)
{