0
0
Fork 0
mirror of https://github.com/denoland/rusty_v8.git synced 2025-01-14 10:02:09 -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);
}
class ExternalOneByteString
: public v8::String::ExternalOneByteStringResource {
class ExternalOneByteString : public v8::String::ExternalOneByteStringResource {
public:
ExternalOneByteString(char* data, int length,
void (*rustFree)(char*, size_t), v8::Isolate* isolate)
: _data(data), _length(length), _rustFree(rustFree), _isolate(isolate) {
using RustDestroy = void (*)(char*, size_t);
ExternalOneByteString(char* data, int length, RustDestroy rustDestroy,
v8::Isolate* isolate)
: _data(data),
_length(length),
_rustDestroy(rustDestroy),
_isolate(isolate) {
_isolate->AdjustAmountOfExternalAllocatedMemory(
static_cast<int64_t>(sizeof(*this) + _length));
}
static_cast<int64_t>(_length));
}
~ExternalOneByteString() override {
(*_rustFree)(_data, _length);
(*_rustDestroy)(_data, _length);
_isolate->AdjustAmountOfExternalAllocatedMemory(
-static_cast<int64_t>(-(sizeof(*this) + _length)));
-static_cast<int64_t>(-_length));
}
const char* data() const override { return _data; }
@ -1093,7 +1096,7 @@ class ExternalOneByteString
private:
char* _data;
const int _length;
void (*_rustFree)(char*, size_t);
RustDestroy _rustDestroy;
v8::Isolate* _isolate;
};
@ -1149,10 +1152,9 @@ const v8::String* v8__String__NewExternalOneByteStatic(v8::Isolate* isolate,
const v8::String* v8__String__NewExternalOneByte(
v8::Isolate* isolate, char* data, int length,
void (*rustFree)(char*, size_t)) {
ExternalOneByteString::RustDestroy rustDestroy) {
return maybe_local_to_ptr(v8::String::NewExternalOneByte(
isolate,
new ExternalOneByteString(data, length, rustFree, isolate)));
isolate, new ExternalOneByteString(data, length, rustDestroy, isolate)));
}
const char* v8__ExternalOneByteStringResource__data(