@@ -5,7 +5,8 @@ void DataLayout::Init(Napi::Env env, Napi::Object &exports) {
5
5
Napi::HandleScope scope (env);
6
6
Napi::Function func = DefineClass (env, " DataLayout" , {
7
7
InstanceMethod (" getStringRepresentation" , &DataLayout::getStringRepresentation),
8
- InstanceMethod (" getTypeAllocSize" , &DataLayout::getTypeAllocSize)
8
+ InstanceMethod (" getTypeAllocSize" , &DataLayout::getTypeAllocSize),
9
+ InstanceMethod (" getTypeAllocSizeInBits" , &DataLayout::getTypeAllocSizeInBits)
9
10
});
10
11
constructor = Napi::Persistent (func);
11
12
constructor.SuppressDestruct ();
@@ -27,10 +28,13 @@ llvm::DataLayout &DataLayout::Extract(const Napi::Value &value) {
27
28
DataLayout::DataLayout (const Napi::CallbackInfo &info) : ObjectWrap(info) {
28
29
Napi::Env env = info.Env ();
29
30
if (!info.IsConstructCall () || info.Length () == 0 ||
30
- !info[0 ].IsExternal () && !info[0 ].IsString ()) {
31
+ !Module::IsClassOf (info[ 0 ]) && ! info[0 ].IsExternal () && !info[0 ].IsString ()) {
31
32
throw Napi::TypeError::New (env, ErrMsg::Class::DataLayout::constructor);
32
33
}
33
- if (info[0 ].IsExternal ()) {
34
+ if (Module::IsClassOf (info[0 ])) {
35
+ llvm::Module *module = Module::Extract (info[0 ]);
36
+ dataLayout = new llvm::DataLayout (module);
37
+ } else if (info[0 ].IsExternal ()) {
34
38
auto external = info[0 ].As <Napi::External<llvm::DataLayout>>();
35
39
dataLayout = external.Data ();
36
40
} else if (info[0 ].IsString ()) {
@@ -57,3 +61,13 @@ Napi::Value DataLayout::getTypeAllocSize(const Napi::CallbackInfo &info) {
57
61
}
58
62
throw Napi::TypeError::New (env, ErrMsg::Class::DataLayout::getTypeAllocSize);
59
63
}
64
+
65
+ Napi::Value DataLayout::getTypeAllocSizeInBits (const Napi::CallbackInfo &info) {
66
+ Napi::Env env = info.Env ();
67
+ if (info.Length () == 1 && Type::IsClassOf (info[0 ])) {
68
+ llvm::Type *type = Type::Extract (info[0 ]);
69
+ auto allocSizeInBits = dataLayout->getTypeAllocSizeInBits (type);
70
+ return Napi::Number::New (env, double (allocSizeInBits.getFixedSize ()));
71
+ }
72
+ throw Napi::TypeError::New (env, ErrMsg::Class::DataLayout::getTypeAllocSize);
73
+ }
0 commit comments