Skip to content

Commit 71336d9

Browse files
committed
Proper inheritance model with DIScope
Signed-off-by: Vihan <[email protected]>
1 parent 2770943 commit 71336d9

File tree

7 files changed

+120
-29
lines changed

7 files changed

+120
-29
lines changed

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
module.exports = require("bindings")("llvm-node.node");
1+
module.exports = require("bindings")("llvm-node.node");

llvm-node.d.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,11 @@ declare namespace llvm {
268268

269269
}
270270

271-
class DIFile {
271+
class DIFile extends DIScope {
272+
private constructor();
273+
}
274+
275+
class DIScope {
272276
readonly filename: string;
273277
readonly directory: string;
274278

src/ir/di-file.cc

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ NAN_MODULE_INIT(DIFileWrapper::Init) {
66
v8::Local<v8::FunctionTemplate> tpl = Nan::New<v8::FunctionTemplate>(New);
77
tpl->SetClassName(Nan::New("DIFile").ToLocalChecked());
88
tpl->InstanceTemplate()->SetInternalFieldCount(1);
9-
10-
Nan::SetAccessor(tpl->InstanceTemplate(), Nan::New("filename").ToLocalChecked(), DIFileWrapper::getFilename);
11-
Nan::SetAccessor(tpl->InstanceTemplate(), Nan::New("directory").ToLocalChecked(), DIFileWrapper::getDirectory);
9+
tpl->Inherit(Nan::New(DIScopeWrapper::functionTemplate));
1210

1311
functionTemplate.Reset(tpl);
1412

@@ -34,18 +32,6 @@ NAN_METHOD(DIFileWrapper::New) {
3432
info.GetReturnValue().Set(info.This());
3533
}
3634

37-
NAN_GETTER(DIFileWrapper::getFilename) {
38-
auto* wrapper = DIFileWrapper::FromValue(info.Holder())->getDIFile();
39-
auto result = Nan::New(wrapper->getFilename().str()).ToLocalChecked();
40-
info.GetReturnValue().Set(result);
41-
}
42-
43-
NAN_GETTER(DIFileWrapper::getDirectory) {
44-
auto* wrapper = DIFileWrapper::FromValue(info.Holder())->getDIFile();
45-
auto result = Nan::New(wrapper->getDirectory().str()).ToLocalChecked();
46-
info.GetReturnValue().Set(result);
47-
}
48-
4935
v8::Local<v8::Object> DIFileWrapper::of(llvm::DIFile *file) {
5036
v8::Local<v8::FunctionTemplate> localFunctionTemplate = Nan::New(functionTemplate);
5137
v8::Local<v8::Object> object = Nan::NewInstance(localFunctionTemplate->InstanceTemplate()).ToLocalChecked();
@@ -58,7 +44,7 @@ v8::Local<v8::Object> DIFileWrapper::of(llvm::DIFile *file) {
5844
}
5945

6046
llvm::DIFile *DIFileWrapper::getDIFile() {
61-
return this->diFile;
47+
return static_cast<llvm::DIFile*>(getDIScope());
6248
}
6349

6450
bool DIFileWrapper::isInstance(v8::Local<v8::Value> value) {

src/ir/di-file.h

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,25 @@
55
#include <llvm/IR/DebugInfoMetadata.h>
66
#include <nan_callbacks_12_inl.h>
77
#include "../util/from-value-mixin.h"
8+
#include "di-scope.h"
89

9-
class DIFileWrapper: public Nan::ObjectWrap, public FromValueMixin<DIFileWrapper> {
10+
class DIFileWrapper: public DIScopeWrapper, public FromValueMixin<DIFileWrapper> {
1011
public:
1112
static NAN_MODULE_INIT(Init);
1213
static v8::Local<v8::Object> of(llvm::DIFile *diFile);
1314

15+
using FromValueMixin<DIFileWrapper>::FromValue;
16+
1417
static bool isInstance(v8::Local<v8::Value> value);
1518

1619
llvm::DIFile *getDIFile();
1720

1821
private:
19-
llvm::DIFile *diFile;
20-
21-
explicit DIFileWrapper(llvm::DIFile *diFile) : diFile { diFile } {
22+
explicit DIFileWrapper(llvm::DIFile *diFile) : DIScopeWrapper { diFile } {
2223
}
2324

2425
static NAN_METHOD(New);
2526

26-
// instance
27-
static NAN_GETTER(getFilename);
28-
static NAN_GETTER(getDirectory);
29-
3027
static Nan::Persistent<v8::FunctionTemplate> functionTemplate;
3128
};
3229

src/ir/di-scope.cc

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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+
}

src/ir/di-scope.h

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#ifndef LLVM_NODE_DI_SCOPE_H
2+
#define LLVM_NODE_DI_SCOPE_H
3+
4+
#include <nan.h>
5+
#include <llvm/IR/DebugInfoMetadata.h>
6+
#include <nan_callbacks_12_inl.h>
7+
#include "../util/from-value-mixin.h"
8+
9+
class DIScopeWrapper: public Nan::ObjectWrap, public FromValueMixin<DIScopeWrapper> {
10+
public:
11+
static NAN_MODULE_INIT(Init);
12+
static v8::Local<v8::Object> of(llvm::DIScope *scope);
13+
14+
static bool isInstance(v8::Local<v8::Value> value);
15+
16+
llvm::DIScope *getDIScope();
17+
18+
protected:
19+
explicit DIScopeWrapper(llvm::DIScope *scope) : scope { scope } {
20+
}
21+
22+
static Nan::Persistent<v8::FunctionTemplate> functionTemplate;
23+
24+
private:
25+
llvm::DIScope *scope;
26+
27+
static NAN_METHOD(New);
28+
29+
// instance
30+
static NAN_GETTER(getFilename);
31+
static NAN_GETTER(getDirectory);
32+
};
33+
34+
#endif //LLVM_NODE_DI_SCOPE_H

src/ir/ir.cc

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "di-builder.h"
1010
#include "di-file.h"
1111
#include "di-compile-unit.h"
12+
#include "di-scope.h"
1213
#include "module.h"
1314
#include "llvm-context.h"
1415
#include "type.h"
@@ -60,13 +61,14 @@ NAN_MODULE_INIT(InitIR) {
6061
ConstantIntWrapper::Init(target);
6162
ConstantPointerNullWrapper::Init(target);
6263
DataLayoutWrapper::Init(target);
64+
DIBuilderWrapper::Init(target);
65+
DICompileUnitWrapper::Init(target);
66+
DIScopeWrapper::Init(target);
67+
DIFileWrapper::Init(target);
6368
FunctionWrapper::Init(target);
6469
FunctionTypeWrapper::Init(target);
6570
GlobalVariableWrapper::Init(target);
6671
IRBuilderWrapper::Init(target);
67-
DIBuilderWrapper::Init(target);
68-
DIFileWrapper::Init(target);
69-
DICompileUnitWrapper::Init(target);
7072
InitLinkageTypes(target);
7173
IntegerTypeWrapper::Init(target);
7274
ModuleWrapper::Init(target);

0 commit comments

Comments
 (0)