-
Notifications
You must be signed in to change notification settings - Fork 0
/
nDll.cpp
759 lines (643 loc) · 20.9 KB
/
nDll.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
// nDll.cpp : 定义 DLL 的导出函数。
//
#include "pch.h"
#include "framework.h"
#include "nDll.h"
#include <SDKDDKVer.h>
#include <assert.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "include/v8.h"
#include "include/libplatform/libplatform.h"
#include <windows.h>
#pragma comment(lib,"v8_base_0.lib")
#pragma comment(lib,"v8_base_1.lib")
#pragma comment(lib,"v8_base_2.lib")
#pragma comment(lib,"v8_base_3.lib")
#pragma comment(lib,"v8_libbase.lib")
#pragma comment(lib,"v8_libplatform.lib")
#pragma comment(lib,"v8_nosnapshot.lib")
#pragma comment(lib,"v8_snapshot.lib")
#pragma comment(lib, "winmm.lib ")
// 这是导出变量的一个示例
NDLL_API int nnDll=0;
// 这是导出函数的一个示例。
NDLL_API int fnnDll(void)
{
return 0;
}
// 这是已导出类的构造函数。
CnDll::CnDll()
{
return;
}
using namespace v8;
#define LocalStringFromUtf8(isolate, s) (String::NewFromUtf8(isolate, s, NewStringType::kNormal).ToLocalChecked())
#define LocalString(isolate, s) (String::NewFromTwoByte(isolate, (const uint16_t*)s, NewStringType::kNormal).ToLocalChecked())
Platform* v8Platform;
class SimpleArrayBufferAllocator : public ArrayBuffer::Allocator {
public:
virtual void* Allocate(size_t length) {
void* data = AllocateUninitialized(length);
return data == nullptr ? data : memset(data, 0, length);
}
virtual void* AllocateUninitialized(size_t length) { return malloc(length); }
virtual void Free(void* data, size_t) { free(data); }
};
SimpleArrayBufferAllocator array_buffer_allocator;
BOOL __stdcall v8_init() {
if (!V8::InitializeICU())
return FALSE;
OutputDebugStringA("InitializeICU ok");
v8Platform = platform::CreateDefaultPlatform();
if (!v8Platform)
return FALSE;
OutputDebugStringA("CreateDefaultPlatform ok");
V8::InitializePlatform(v8Platform);
OutputDebugStringA("InitializePlatform ok");
if (!V8::Initialize())
{
OutputDebugStringA("Initialize fail");
delete v8Platform;
v8Platform = nullptr;
return FALSE;
}
return TRUE;
}
void __stdcall v8_cleanup() {
V8::Dispose();
V8::ShutdownPlatform();
delete v8Platform;
v8Platform = nullptr;
}
V8Isolate __stdcall v8_new_isolate() {
Isolate::CreateParams create_params;
create_params.array_buffer_allocator = &array_buffer_allocator;
return (V8Isolate)Isolate::New(create_params);
}
void __stdcall v8_destroy_isolate(V8Isolate isolate) {
((Isolate*)isolate)->Dispose();
}
void __stdcall v8_enter_isolate(V8Isolate isolate) {
((Isolate*)isolate)->Enter();
}
void __stdcall v8_leave_isolate(V8Isolate isolate) {
((Isolate*)isolate)->Exit();
}
void __stdcall v8_throw_exception(int type, const uint16_t* errmsg)
{
Isolate* isolate = Isolate::GetCurrent();
HandleScope scope(isolate);
switch (type) {
case V8_RANGE_ERROR:
isolate->ThrowException(Exception::RangeError(LocalString(isolate, errmsg)));
break;
case V8_REFERENCE_ERROR:
isolate->ThrowException(Exception::ReferenceError(LocalString(isolate, errmsg)));
break;
case V8_SYNTAX_ERROR:
isolate->ThrowException(Exception::SyntaxError(LocalString(isolate, errmsg)));
break;
case V8_TYPE_ERROR:
isolate->ThrowException(Exception::TypeError(LocalString(isolate, errmsg)));
break;
default:
isolate->ThrowException(Exception::Error(LocalString(isolate, errmsg)));
break;
}
}
V8Context __stdcall v8_new_context(V8Isolate _isolate) {
Isolate* isolate = (Isolate*)_isolate;
HandleScope handle_scope(isolate);
Local<Context> context = Context::New(isolate);
return new Global<Context>(isolate, context);
}
void __stdcall v8_enter_context(V8Context _context) {
auto context = (Global<Context>*)_context;
auto isolate = Isolate::GetCurrent();
HandleScope handleScope(isolate);
Local<Context> lcontext = Local<Context>::New(isolate, *context);
lcontext->Enter();
}
void __stdcall v8_leave_context(V8Context _context) {
auto context = (Global<Context>*)_context;
auto isolate = Isolate::GetCurrent();
HandleScope handleScope(isolate);
Local<Context> lcontext = Local<Context>::New(isolate, *context);
lcontext->Exit();
}
void __stdcall v8_destroy_context(V8Context context) {
delete (Global<Context>*)context;
}
V8Object __stdcall v8_global_object(V8Context _context) {
auto context = (Global<Context>*)_context;
auto isolate = Isolate::GetCurrent();
HandleScope handleScope(isolate);
Local<Context> lcontext = Local<Context>::New(isolate, *context);
return (V8Object)new Global<Object>(isolate, lcontext->Global());
}
const uint16_t* __stdcall v8_strinfo(V8String _str, int* len)
{
auto str = (String::Value*)_str;
if (len)
*len = str->length();
return **str;
}
String::Value* __stdcall v8_val_to_string(const Local<Value>* value)
{
return new String::Value(*value);
}
void __stdcall v8_destroy_string(V8String p)
{
delete (String::Value*)p;
}
const char* ToCString(const String::Utf8Value& value) {
return *value ? *value : "<string conversion failed>";
}
void ReportException(Isolate* isolate, TryCatch* try_catch) {
HandleScope handle_scope(isolate);
String::Utf8Value exception(try_catch->Exception());
const char* exception_string = ToCString(exception);
Local<Message> message = try_catch->Message();
if (message.IsEmpty()) {
// V8 didn't provide any extra information about this error; just
// print the exception.
OutputDebugStringA(exception_string);
}
else {
// Print (filename):(line number): (message).
String::Utf8Value filename(message->GetScriptOrigin().ResourceName());
Local<Context> context(isolate->GetCurrentContext());
const char* filename_string = ToCString(filename);
int linenum = message->GetLineNumber(context).FromJust();
char buf[4096];
sprintf_s(buf, "%s:%i: %s\n", filename_string, linenum, exception_string);
OutputDebugStringA(buf);
// Print line of source code.
String::Utf8Value sourceline(
message->GetSourceLine(context).ToLocalChecked());
const char* sourceline_string = ToCString(sourceline);
OutputDebugStringA(sourceline_string);
// Print wavy underline (GetUnderline is deprecated).
int start = message->GetStartColumn(context).FromJust();
for (int i = 0; i < start; i++) {
OutputDebugStringA(" ");
}
int end = message->GetEndColumn(context).FromJust();
for (int i = start; i < end; i++) {
OutputDebugStringA("^");
}
OutputDebugStringA("\n");
Local<Value> stack_trace_string;
if (try_catch->StackTrace(context).ToLocal(&stack_trace_string) &&
stack_trace_string->IsString() &&
Local<String>::Cast(stack_trace_string)->Length() > 0) {
String::Utf8Value stack_trace(stack_trace_string);
const char* stack_trace_string = ToCString(stack_trace);
OutputDebugStringA(stack_trace_string);
}
}
}
V8String __stdcall v8_eval_asstr(V8Isolate _isolate, V8Context _context, const uint16_t* code)
{
auto isolate = (Isolate*)_isolate;
auto context = (Global<Context>*)_context;
if (!isolate)
isolate = Isolate::GetCurrent();
if (!isolate)
return nullptr;
HandleScope handle_scope(isolate);
Local<Context> lcontext;
if (context)
lcontext = Local<Context>::New(isolate, *context);
else
lcontext = isolate->GetCurrentContext();
Context::Scope context_scope(lcontext);
TryCatch tryCatch(isolate);
Local<String> source = LocalString(isolate, code);
MaybeLocal<Script> script = Script::Compile(lcontext, source);
if (script.IsEmpty())
{
OutputDebugStringA("Compile error");
ReportException(isolate, &tryCatch);
return (V8String)new String::Value(tryCatch.Exception());
}
MaybeLocal<Value> result = script.ToLocalChecked()->Run(lcontext);
if (result.IsEmpty())
{
OutputDebugStringA("Run error");
ReportException(isolate, &tryCatch);
return (V8String)new String::Value(tryCatch.Exception());
}
auto lresult = result.ToLocalChecked();
return (V8String)v8_val_to_string(&lresult);
}
BOOL __stdcall v8_set_object(
V8Isolate _isolate,
V8Context _context,
const uint16_t* propName,
V8Object _owner,
V8Object _propValue) {
auto isolate = (Isolate*)_isolate;
auto context = (Global<Context>*)_context;
auto owner = (Global<Object>*)_owner;
auto propValue = (Global<Object>*)_propValue;
if (!isolate)
isolate = Isolate::GetCurrent();
if (!isolate)
return FALSE;
HandleScope handle_scope(isolate);
Local<Context> lcontext;
if (context)
lcontext = Local<Context>::New(isolate, *context);
else
lcontext = isolate->GetCurrentContext();
Context::Scope context_scope(lcontext);
Local<Object> obj;
if (owner)
obj = Local<Object>::New(isolate, *owner);
else
obj = lcontext->Global();
auto result = obj->Set(lcontext, LocalString(isolate, propName), Local<Object>::New(isolate, *propValue));
return result.FromMaybe(false);
}
BOOL __stdcall v8_register_native_function(
V8Isolate _isolate,
V8Context _context,
const char* funcname,
V8FunctionCallback func,
const void* data) {
auto isolate = (Isolate*)_isolate;
auto context = (Global<Context>*)_context;
if (!isolate)
isolate = Isolate::GetCurrent();
if (!isolate)
return FALSE;
HandleScope handle_scope(isolate);
Local<Context> lcontext;
if (context)
lcontext = Local<Context>::New(isolate, *context);
else
lcontext = isolate->GetCurrentContext();
Context::Scope context_scope(lcontext);
Local<Object> global = lcontext->Global();
auto result = global->Set(lcontext, LocalStringFromUtf8(isolate, funcname),
Function::New(lcontext, (FunctionCallback)func, External::New(isolate, (void*)data)).ToLocalChecked());
return result.FromMaybe(false);
}
void* __stdcall v8_FunctionCallbackInfo_data(const V8FunctionCallbackInfo _info) {
auto info = (const FunctionCallbackInfo<v8::Value>*)_info;
auto data = info->Data();
if (data->IsExternal())
{
auto tmp = Local<External>::Cast(data);
return tmp->Value();
}
else
return nullptr;
}
V8Object __stdcall v8_FunctionCallbackInfo_this(const V8FunctionCallbackInfo _info) {
auto info = (const FunctionCallbackInfo<v8::Value>*)_info;
auto isolate = info->GetIsolate();
HandleScope handleScope(isolate);
return new Global<Object>(isolate, info->This());
}
void* __stdcall v8_FunctionCallbackInfo_internal_field(const V8FunctionCallbackInfo _info, int idx) {
auto info = (const FunctionCallbackInfo<v8::Value>*)_info;
HandleScope handleScope(info->GetIsolate());
Local<Object> holder = info->Holder();
if (holder->InternalFieldCount() > idx) {
Local<External> field = Local<External>::Cast(holder->GetInternalField(idx));
if (field.IsEmpty())
return nullptr;
else
return field->Value();
}
else
return nullptr;
}
int32_t __stdcall v8_FunctionCallbackInfo_arg_count(const V8FunctionCallbackInfo _info)
{
auto info = (const FunctionCallbackInfo<v8::Value>*)_info;
return info->Length();
}
V8String __stdcall v8_FunctionCallbackInfo_arg_as_str(const V8FunctionCallbackInfo _info, int idx) {
auto info = (const FunctionCallbackInfo<v8::Value>*)_info;
auto arg = (*info)[idx];
return (V8String)v8_val_to_string(&arg);
}
BOOL __stdcall v8_FunctionCallbackInfo_arg_as_int32(
const V8FunctionCallbackInfo _info,
int idx, int32_t* result) {
auto info = (const FunctionCallbackInfo<v8::Value>*)_info;
auto isolate = info->GetIsolate();
HandleScope handleScope(isolate);
auto arg = (*info)[idx];
auto value = arg->Int32Value(isolate->GetCurrentContext());
if (value.IsJust())
{
if (result)
*result = value.FromJust();
return TRUE;
}
else
{
return false;
}
}
BOOL __stdcall v8_FunctionCallbackInfo_arg_as_uint32(
const V8FunctionCallbackInfo _info,
int idx, uint32_t* result) {
auto info = (const FunctionCallbackInfo<v8::Value>*)_info;
auto isolate = info->GetIsolate();
HandleScope handleScope(isolate);
auto arg = (*info)[idx];
auto value = arg->Uint32Value(isolate->GetCurrentContext());
if (value.IsJust())
{
if (result)
*result = value.FromJust();
return TRUE;
}
else
{
return false;
}
}
BOOL __stdcall v8_FunctionCallbackInfo_arg_as_int64(
const V8FunctionCallbackInfo _info,
int idx, int64_t* result) {
auto info = (const FunctionCallbackInfo<v8::Value>*)_info;
auto isolate = info->GetIsolate();
HandleScope handleScope(isolate);
auto arg = (*info)[idx];
auto value = arg->IntegerValue(isolate->GetCurrentContext());
if (value.IsJust())
{
if (result)
*result = value.FromJust();
return TRUE;
}
else
{
return false;
}
}
BOOL __stdcall v8_FunctionCallbackInfo_arg_as_float(
const V8FunctionCallbackInfo _info,
int idx, double* result) {
auto info = (const FunctionCallbackInfo<v8::Value>*)_info;
auto isolate = info->GetIsolate();
HandleScope handleScope(isolate);
auto arg = (*info)[idx];
auto value = arg->NumberValue(isolate->GetCurrentContext());
if (value.IsJust())
{
if (result)
*result = value.FromJust();
return TRUE;
}
else
{
return false;
}
}
V8Object __stdcall v8_FunctionCallbackInfo_arg_as_object(const V8FunctionCallbackInfo _info, int idx)
{
auto info = (const FunctionCallbackInfo<v8::Value>*)_info;
auto isolate = info->GetIsolate();
HandleScope handleScope(isolate);
auto context = isolate->GetCurrentContext();
auto arg = (*info)[idx];
auto tmp = arg->ToObject(context);
if (tmp.IsEmpty())
return nullptr;
else
return (V8Object)new Global<Object>(isolate, tmp.ToLocalChecked());
}
void __stdcall v8_FunctionCallbackInfo_return_int32(const V8FunctionCallbackInfo _info, int32_t result) {
auto info = (const FunctionCallbackInfo<v8::Value>*)_info;
info->GetReturnValue().Set(result);
}
void __stdcall v8_FunctionCallbackInfo_return_uint32(const V8FunctionCallbackInfo _info, uint32_t result) {
auto info = (const FunctionCallbackInfo<v8::Value>*)_info;
info->GetReturnValue().Set(result);
}
void __stdcall v8_FunctionCallbackInfo_return_int64(const V8FunctionCallbackInfo _info, int64_t* result) {
auto info = (const FunctionCallbackInfo<v8::Value>*)_info;
info->GetReturnValue().Set(Number::New(info->GetIsolate(), (double)*result));
}
void __stdcall v8_FunctionCallbackInfo_return_float(const V8FunctionCallbackInfo _info, double result) {
auto info = (const FunctionCallbackInfo<v8::Value>*)_info;
info->GetReturnValue().Set(result);
}
void __stdcall v8_FunctionCallbackInfo_return_string(const V8FunctionCallbackInfo _info, const uint16_t* result) {
auto info = (const FunctionCallbackInfo<v8::Value>*)_info;
info->GetReturnValue().Set(LocalString(info->GetIsolate(), result));
}
V8ObjectTemplate __stdcall v8_new_object_template(V8Isolate _isolate, int InternalFieldCount) {
auto isolate = (Isolate*)_isolate;
if (!isolate)
isolate = Isolate::GetCurrent();
if (!isolate)
return nullptr;
HandleScope handle_scope(isolate);
Local<ObjectTemplate> objTemplate = ObjectTemplate::New(isolate);
objTemplate->SetInternalFieldCount(InternalFieldCount);
return (V8ObjectTemplate)new Global<ObjectTemplate>(isolate, objTemplate);
}
void __stdcall v8_destroy_object_template(V8ObjectTemplate objTemplate) {
delete (Global<ObjectTemplate>*)objTemplate;
}
BOOL __stdcall v8_object_template_add_method(V8Isolate _isolate, V8Context _context, V8ObjectTemplate _objTemplate,
const char* name, V8FunctionCallback func, const void* data) {
auto isolate = (Isolate*)_isolate;
auto context = (Global<Context>*)_context;
auto objTemplate = (Global<ObjectTemplate>*)_objTemplate;
if (!isolate)
isolate = Isolate::GetCurrent();
if (!isolate)
return FALSE;
HandleScope handle_scope(isolate);
Local<ObjectTemplate> tmpl = Local<ObjectTemplate>::New(isolate, *objTemplate);
Local<Context> lcontext;
if (context)
lcontext = Local<Context>::New(isolate, *context);
else
lcontext = isolate->GetCurrentContext();
Context::Scope context_scope(lcontext);
auto funcObj = Function::New(lcontext, (FunctionCallback)func, External::New(isolate, (void*)data));
if (funcObj.IsEmpty())
return FALSE;
else {
tmpl->Set(isolate, name, funcObj.ToLocalChecked());
return TRUE;
}
}
V8Object __stdcall v8_new_object(V8Isolate _isolate, V8Context _context,
V8ObjectTemplate _objTemplate, void* FirstInternalField) {
auto isolate = (Isolate*)_isolate;
auto context = (Global<Context>*)_context;
auto objTemplate = (Global<ObjectTemplate>*)_objTemplate;
if (!isolate)
isolate = Isolate::GetCurrent();
if (!isolate)
return nullptr;
HandleScope handle_scope(isolate);
Local<Context> lcontext;
if (context)
lcontext = Local<Context>::New(isolate, *context);
else
lcontext = isolate->GetCurrentContext();
Context::Scope context_scope(lcontext);
Local<ObjectTemplate> tmpl = Local<ObjectTemplate>::New(isolate, *objTemplate);
auto obj = tmpl->NewInstance(lcontext);
if (obj.IsEmpty())
return nullptr;
else {
auto result = obj.ToLocalChecked();
if (result->InternalFieldCount() > 0)
result->SetInternalField(0, External::New(isolate, FirstInternalField));
return new Global<Object>(isolate, result);
}
}
void __stdcall v8_destroy_object(V8Object obj) {
delete (Global<Object>*)obj;
}
int __stdcall v8_object_internal_field_count(V8Object _obj) {
auto obj = (Global<Object>*)_obj;
Isolate* isolate = Isolate::GetCurrent();
HandleScope handleScope(isolate);
Local<Object> lobj = Local<Object>::New(isolate, *obj);
return lobj->InternalFieldCount();
}
void* __stdcall v8_object_get_internal_field(V8Object _obj, int idx) {
auto obj = (Global<Object>*)_obj;
Isolate* isolate = Isolate::GetCurrent();
HandleScope handleScope(isolate);
Local<Object> lobj = Local<Object>::New(isolate, *obj);
if (lobj->InternalFieldCount() > idx) {
Local<External> field = Local<External>::Cast(lobj->GetInternalField(idx));
if (field.IsEmpty())
return nullptr;
else
return field->Value();
}
else
return nullptr;
}
void __stdcall v8_object_set_internal_field(V8Object _obj, int idx, void* value) {
auto obj = (Global<Object>*)_obj;
Isolate* isolate = Isolate::GetCurrent();
HandleScope handleScope(isolate);
Local<Object> lobj = Local<Object>::New(isolate, *obj);
if (lobj->InternalFieldCount() > idx)
lobj->SetInternalField(idx, External::New(isolate, value));
}
int32_t __stdcall v8_object_get_int32_field(V8Object _obj, const uint16_t* name, int32_t defValue) {
auto obj = (Global<Object>*)_obj;
Isolate* isolate = Isolate::GetCurrent();
HandleScope handleScope(isolate);
auto context = isolate->GetCurrentContext();
Local<Object> lobj = Local<Object>::New(isolate, *obj);
auto result = lobj->Get(isolate->GetCurrentContext(), LocalString(isolate, name));
if (result.IsEmpty())
return defValue;
else {
auto tmp = result.ToLocalChecked()->Int32Value(context);
if (tmp.IsJust())
return tmp.FromJust();
else
return defValue;
}
}
uint32_t __stdcall v8_object_get_uint32_field(V8Object _obj, const uint16_t* name, uint32_t defValue) {
auto obj = (Global<Object>*)_obj;
Isolate* isolate = Isolate::GetCurrent();
HandleScope handleScope(isolate);
auto context = isolate->GetCurrentContext();
Local<Object> lobj = Local<Object>::New(isolate, *obj);
auto result = lobj->Get(isolate->GetCurrentContext(), LocalString(isolate, name));
if (result.IsEmpty())
return defValue;
else {
auto tmp = result.ToLocalChecked()->Uint32Value(context);
if (tmp.IsJust())
return tmp.FromJust();
else
return defValue;
}
}
BOOL __stdcall v8_object_get_float_field(V8Object _obj, const uint16_t* name, double* value) {
auto obj = (Global<Object>*)_obj;
Isolate* isolate = Isolate::GetCurrent();
HandleScope handleScope(isolate);
auto context = isolate->GetCurrentContext();
Local<Object> lobj = Local<Object>::New(isolate, *obj);
auto result = lobj->Get(context, LocalString(isolate, name));
if (result.IsEmpty())
return false;
else {
auto tmp = result.ToLocalChecked()->NumberValue(context);
if (tmp.IsJust())
{
*value = tmp.FromJust();
return true;
}
else
return false;
}
}
BOOL __stdcall v8_object_get_int64_field(V8Object _obj, const uint16_t* name, int64_t* value) {
auto obj = (Global<Object>*)_obj;
Isolate* isolate = Isolate::GetCurrent();
HandleScope handleScope(isolate);
auto context = isolate->GetCurrentContext();
Local<Object> lobj = Local<Object>::New(isolate, *obj);
auto result = lobj->Get(context, LocalString(isolate, name));
if (result.IsEmpty())
return false;
else {
auto tmp = result.ToLocalChecked()->IntegerValue(context);
if (tmp.IsJust())
{
*value = tmp.FromJust();
return true;
}
else
return false;
}
}
V8String __stdcall v8_object_get_string_field(V8Object _obj, const uint16_t* name) {
auto obj = (Global<Object>*)_obj;
Isolate* isolate = Isolate::GetCurrent();
HandleScope handleScope(isolate);
auto context = isolate->GetCurrentContext();
Local<Object> lobj = Local<Object>::New(isolate, *obj);
auto result = lobj->Get(context, LocalString(isolate, name));
if (result.IsEmpty())
return nullptr;
else {
auto tmp = result.ToLocalChecked();
return v8_val_to_string(&tmp);
}
}
V8Object __stdcall v8_object_get_object_field(V8Object _obj, const uint16_t* name) {
auto obj = (Global<Object>*)_obj;
Isolate* isolate = Isolate::GetCurrent();
HandleScope handleScope(isolate);
auto context = isolate->GetCurrentContext();
Local<Object> lobj = Local<Object>::New(isolate, *obj);
auto result = lobj->Get(context, LocalString(isolate, name));
if (result.IsEmpty())
return nullptr;
else {
auto tmp = result.ToLocalChecked()->ToObject(context);
if (tmp.IsEmpty())
return nullptr;
else
return (V8Object)new Global<Object>(isolate, tmp.ToLocalChecked());
}
}