0
0
Fork 0
mirror of https://github.com/denoland/rusty_v8.git synced 2025-01-14 18:09:05 -05:00

Cleanup binding + fix adjust memory

This commit is contained in:
Nathan Whitaker 2024-06-27 12:04:34 -07:00
parent b313721245
commit b085881ac4
No known key found for this signature in database

View file

@ -1071,19 +1071,22 @@ v8__String__GetExternalStringResourceBase(const v8::String& self,
return self.GetExternalStringResourceBase(encoding_out); return self.GetExternalStringResourceBase(encoding_out);
} }
class ExternalOneByteString class ExternalOneByteString : public v8::String::ExternalOneByteStringResource {
: public v8::String::ExternalOneByteStringResource {
public: public:
ExternalOneByteString(char* data, int length, using RustDestroy = void (*)(char*, size_t);
void (*rustFree)(char*, size_t), v8::Isolate* isolate) ExternalOneByteString(char* data, int length, RustDestroy rustDestroy,
: _data(data), _length(length), _rustFree(rustFree), _isolate(isolate) { v8::Isolate* isolate)
: _data(data),
_length(length),
_rustDestroy(rustDestroy),
_isolate(isolate) {
_isolate->AdjustAmountOfExternalAllocatedMemory( _isolate->AdjustAmountOfExternalAllocatedMemory(
static_cast<int64_t>(sizeof(*this) + _length)); static_cast<int64_t>(_length));
} }
~ExternalOneByteString() override { ~ExternalOneByteString() override {
(*_rustFree)(_data, _length); (*_rustDestroy)(_data, _length);
_isolate->AdjustAmountOfExternalAllocatedMemory( _isolate->AdjustAmountOfExternalAllocatedMemory(
-static_cast<int64_t>(-(sizeof(*this) + _length))); -static_cast<int64_t>(-_length));
} }
const char* data() const override { return _data; } const char* data() const override { return _data; }
@ -1093,7 +1096,7 @@ class ExternalOneByteString
private: private:
char* _data; char* _data;
const int _length; const int _length;
void (*_rustFree)(char*, size_t); RustDestroy _rustDestroy;
v8::Isolate* _isolate; v8::Isolate* _isolate;
}; };
@ -1149,10 +1152,9 @@ const v8::String* v8__String__NewExternalOneByteStatic(v8::Isolate* isolate,
const v8::String* v8__String__NewExternalOneByte( const v8::String* v8__String__NewExternalOneByte(
v8::Isolate* isolate, char* data, int length, v8::Isolate* isolate, char* data, int length,
void (*rustFree)(char*, size_t)) { ExternalOneByteString::RustDestroy rustDestroy) {
return maybe_local_to_ptr(v8::String::NewExternalOneByte( return maybe_local_to_ptr(v8::String::NewExternalOneByte(
isolate, isolate, new ExternalOneByteString(data, length, rustDestroy, isolate)));
new ExternalOneByteString(data, length, rustFree, isolate)));
} }
const char* v8__ExternalOneByteStringResource__data( const char* v8__ExternalOneByteStringResource__data(