|
| 1 | +#include "di-scope.h" |
| 2 | + |
| 3 | +Nan::Persistent<v8::FunctionTemplate> DIScopeWrapper::functionTemplate {}; |
| 4 | + |
| 5 | +NAN_MODULE_INIT(DIScopeWrapper::Init) { |
| 6 | + v8::Local<v8::FunctionTemplate> tpl = Nan::New<v8::FunctionTemplate>(New); |
| 7 | + tpl->SetClassName(Nan::New("DIScope").ToLocalChecked()); |
| 8 | + tpl->InstanceTemplate()->SetInternalFieldCount(1); |
| 9 | + |
| 10 | + Nan::SetAccessor(tpl->InstanceTemplate(), Nan::New("filename").ToLocalChecked(), DIScopeWrapper::getFilename); |
| 11 | + Nan::SetAccessor(tpl->InstanceTemplate(), Nan::New("directory").ToLocalChecked(), DIScopeWrapper::getDirectory); |
| 12 | + |
| 13 | + functionTemplate.Reset(tpl); |
| 14 | + |
| 15 | + Nan::Set(target, Nan::New("DIScope").ToLocalChecked(), Nan::GetFunction(tpl).ToLocalChecked()); |
| 16 | +} |
| 17 | + |
| 18 | + |
| 19 | +NAN_METHOD(DIScopeWrapper::New) { |
| 20 | + if (!info.IsConstructCall()) |
| 21 | + { |
| 22 | + return Nan::ThrowTypeError("Class Constructor Value cannot be invoked without new"); |
| 23 | + } |
| 24 | + |
| 25 | + if (info.Length() != 1 || !info[0]->IsExternal()) |
| 26 | + { |
| 27 | + return Nan::ThrowTypeError("External Value Pointer required"); |
| 28 | + } |
| 29 | + |
| 30 | + llvm::DIScope *scope = static_cast<llvm::DIScope *>(v8::External::Cast(*info[0])->Value()); |
| 31 | + auto *wrapper = new DIScopeWrapper { scope }; |
| 32 | + |
| 33 | + wrapper->Wrap(info.This()); |
| 34 | + info.GetReturnValue().Set(info.This()); |
| 35 | +} |
| 36 | + |
| 37 | +NAN_GETTER(DIScopeWrapper::getFilename) { |
| 38 | + auto* wrapper = DIScopeWrapper::FromValue(info.Holder())->getDIScope(); |
| 39 | + auto result = Nan::New(wrapper->getFilename().str()).ToLocalChecked(); |
| 40 | + |
| 41 | + info.GetReturnValue().Set(result); |
| 42 | +} |
| 43 | + |
| 44 | +NAN_GETTER(DIScopeWrapper::getDirectory) { |
| 45 | + auto* wrapper = DIScopeWrapper::FromValue(info.Holder())->getDIScope(); |
| 46 | + auto result = Nan::New(wrapper->getDirectory().str()).ToLocalChecked(); |
| 47 | + |
| 48 | + info.GetReturnValue().Set(result); |
| 49 | +} |
| 50 | + |
| 51 | +v8::Local<v8::Object> DIScopeWrapper::of(llvm::DIScope *scope) { |
| 52 | + v8::Local<v8::FunctionTemplate> localFunctionTemplate = Nan::New(functionTemplate); |
| 53 | + v8::Local<v8::Object> object = Nan::NewInstance(localFunctionTemplate->InstanceTemplate()).ToLocalChecked(); |
| 54 | + |
| 55 | + DIScopeWrapper* wrapper = new DIScopeWrapper { scope }; |
| 56 | + wrapper->Wrap(object); |
| 57 | + |
| 58 | + Nan::EscapableHandleScope escapeScope {}; |
| 59 | + return escapeScope.Escape(object); |
| 60 | +} |
| 61 | + |
| 62 | +llvm::DIScope *DIScopeWrapper::getDIScope() { |
| 63 | + return this->scope; |
| 64 | +} |
| 65 | + |
| 66 | +bool DIScopeWrapper::isInstance(v8::Local<v8::Value> value) { |
| 67 | + return Nan::New(functionTemplate)->HasInstance(value); |
| 68 | +} |
0 commit comments