節點版本:v5.1.1 os:window7 x86在javascript和原生插件之間共享緩衝區對象,用C++編寫?
我可以在javascript和本地插件之間共享緩衝區對象嗎?
嘗試以下,但失敗;
C++代碼插件:
void setBuffer(const v8::FunctionCallbackInfo<v8::Value>& args) {
Isolate* isolate = args.GetIsolate();
char* jsBuffer = node::Buffer::Data(Local<Object>::Cast(args[0]));
memcpy(jsBuffer, "bbbbbbbbbb", 10);
printf("native addon output:%s\n", jsBuffer);
}
JavaScript代碼:
var native = require('nativeAddon');
var globalBuffer = new Buffer(20).fill('a');
native.setBuffer(globalBuffer); //set globalBuffer to 'bbbbbb....', using memcpy
console.log("js output:" + globalBuffer);
結果:
本地插件輸出:bbbbbbbbbbaaaaaaaaaaaaaaaa
JS輸出:aaaaaaaaaaaaaaaaaaaaaaaaaa
但globalBuffer沒有改變,爲什麼?
你可能用'node :: Buffer :: Data'創建一個新的緩衝區嗎? – tadman
只是轉換爲char *; – csy2002