summaryrefslogtreecommitdiff
path: root/libdeno/binding.cc
blob: 4c90e699316e8e3bb3867cf62dda1cf19471b236 (plain)
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
// Copyright 2018 the Deno authors. All rights reserved. MIT license.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <string>

#include "third_party/v8/include/libplatform/libplatform.h"
#include "third_party/v8/include/v8.h"
#include "third_party/v8/src/base/logging.h"

#include "deno.h"
#include "internal.h"

#define GLOBAL_IMPORT_BUF_SIZE 1024

namespace deno {

Deno* FromIsolate(v8::Isolate* isolate) {
  return static_cast<Deno*>(isolate->GetData(0));
}

void LazilyCreateDataMap(Deno* d) {
  if (d->async_data_map.IsEmpty()) {
    v8::HandleScope handle_scope(d->isolate);
    // It's important for security reasons that async_data_map is not exposed to
    // the VM.
    auto async_data_map = v8::Map::New(d->isolate);
    d->async_data_map.Reset(d->isolate, async_data_map);
  }
  DCHECK(!d->async_data_map.IsEmpty());
}

void AddDataRef(Deno* d, int32_t req_id, v8::Local<v8::Value> data_v) {
  LazilyCreateDataMap(d);
  auto async_data_map = d->async_data_map.Get(d->isolate);
  auto context = d->context.Get(d->isolate);
  auto req_id_v = v8::Integer::New(d->isolate, req_id);
  auto r = async_data_map->Set(context, req_id_v, data_v);
  CHECK(!r.IsEmpty());
}

void DeleteDataRef(Deno* d, int32_t req_id) {
  LazilyCreateDataMap(d);
  auto context = d->context.Get(d->isolate);
  // Delete persistent reference to data ArrayBuffer.
  auto async_data_map = d->async_data_map.Get(d->isolate);
  auto req_id_v = v8::Integer::New(d->isolate, req_id);
  auto maybe_deleted = async_data_map->Delete(context, req_id_v);
  CHECK(maybe_deleted.IsJust());
}

// Extracts a C string from a v8::V8 Utf8Value.
const char* ToCString(const v8::String::Utf8Value& value) {
  return *value ? *value : "<string conversion failed>";
}

static inline v8::Local<v8::String> v8_str(const char* x) {
  return v8::String::NewFromUtf8(v8::Isolate::GetCurrent(), x,
                                 v8::NewStringType::kNormal)
      .ToLocalChecked();
}

void HandleExceptionStr(v8::Local<v8::Context> context,
                        v8::Local<v8::Value> exception,
                        std::string* exception_str) {
  auto* isolate = context->GetIsolate();
  Deno* d = FromIsolate(isolate);

  v8::HandleScope handle_scope(isolate);
  v8::Context::Scope context_scope(context);

  auto message = v8::Exception::CreateMessage(isolate, exception);
  auto stack_trace = message->GetStackTrace();
  auto line =
      v8::Integer::New(isolate, message->GetLineNumber(context).FromJust());
  auto column =
      v8::Integer::New(isolate, message->GetStartColumn(context).FromJust());

  auto global_error_handler = d->global_error_handler.Get(isolate);

  if (!global_error_handler.IsEmpty()) {
    // global_error_handler is set so we try to handle the exception in
    // javascript.
    v8::Local<v8::Value> args[5];
    args[0] = exception->ToString(context).ToLocalChecked();
    args[1] = message->GetScriptResourceName();
    args[2] = line;
    args[3] = column;
    args[4] = exception;
    global_error_handler->Call(context->Global(), 5, args);
    /* message, source, lineno, colno, error */

    return;
  }

  char buf[12 * 1024];
  if (!stack_trace.IsEmpty()) {
    // No javascript error handler, but we do have a stack trace. Format it
    // into a string and add to last_exception.
    std::string msg;
    v8::String::Utf8Value exceptionStr(isolate, exception);
    msg += ToCString(exceptionStr);
    msg += "\n";

    for (int i = 0; i < stack_trace->GetFrameCount(); ++i) {
      auto frame = stack_trace->GetFrame(isolate, i);
      v8::String::Utf8Value script_name(isolate, frame->GetScriptName());
      int l = frame->GetLineNumber();
      int c = frame->GetColumn();
      snprintf(buf, sizeof(buf), "%s %d:%d\n", ToCString(script_name), l, c);
      msg += buf;
    }
    *exception_str += msg;
  } else {
    // No javascript error handler, no stack trace. Format the little info we
    // have into a string and add to last_exception.
    v8::String::Utf8Value exceptionStr(isolate, exception);
    v8::String::Utf8Value script_name(isolate,
                                      message->GetScriptResourceName());
    v8::String::Utf8Value line_str(isolate, line);
    v8::String::Utf8Value col_str(isolate, column);
    snprintf(buf, sizeof(buf), "%s\n%s %s:%s\n", ToCString(exceptionStr),
             ToCString(script_name), ToCString(line_str), ToCString(col_str));
    *exception_str += buf;
  }
}

void HandleException(v8::Local<v8::Context> context,
                     v8::Local<v8::Value> exception) {
  v8::Isolate* isolate = context->GetIsolate();
  Deno* d = FromIsolate(isolate);
  std::string exception_str;
  HandleExceptionStr(context, exception, &exception_str);
  if (d != nullptr) {
    d->last_exception = exception_str;
  } else {
    std::cerr << "Pre-Deno Exception " << exception_str << std::endl;
    exit(1);
  }
}

const char* PromiseRejectStr(enum v8::PromiseRejectEvent e) {
  switch (e) {
    case v8::PromiseRejectEvent::kPromiseRejectWithNoHandler:
      return "RejectWithNoHandler";
    case v8::PromiseRejectEvent::kPromiseHandlerAddedAfterReject:
      return "HandlerAddedAfterReject";
    case v8::PromiseRejectEvent::kPromiseResolveAfterResolved:
      return "ResolveAfterResolved";
    case v8::PromiseRejectEvent::kPromiseRejectAfterResolved:
      return "RejectAfterResolved";
  }
}

void PromiseRejectCallback(v8::PromiseRejectMessage promise_reject_message) {
  auto* isolate = v8::Isolate::GetCurrent();
  Deno* d = static_cast<Deno*>(isolate->GetData(0));
  DCHECK_EQ(d->isolate, isolate);
  v8::HandleScope handle_scope(d->isolate);
  auto exception = promise_reject_message.GetValue();
  auto context = d->context.Get(d->isolate);
  auto promise = promise_reject_message.GetPromise();
  auto event = promise_reject_message.GetEvent();

  v8::Context::Scope context_scope(context);
  auto promise_reject_handler = d->promise_reject_handler.Get(isolate);

  if (!promise_reject_handler.IsEmpty()) {
    v8::Local<v8::Value> args[3];
    args[1] = v8_str(PromiseRejectStr(event));
    args[2] = promise;
    /* error, event, promise */
    if (event == v8::PromiseRejectEvent::kPromiseRejectWithNoHandler) {
      d->pending_promise_events++;
      // exception only valid for kPromiseRejectWithNoHandler
      args[0] = exception;
    } else if (event ==
               v8::PromiseRejectEvent::kPromiseHandlerAddedAfterReject) {
      d->pending_promise_events--;  // unhandled event cancelled
      if (d->pending_promise_events < 0) {
        d->pending_promise_events = 0;
      }
      // Placeholder, not actually used
      args[0] = v8_str("Promise handler added");
    } else if (event == v8::PromiseRejectEvent::kPromiseResolveAfterResolved) {
      d->pending_promise_events++;
      args[0] = v8_str("Promise resolved after resolved");
    } else if (event == v8::PromiseRejectEvent::kPromiseRejectAfterResolved) {
      d->pending_promise_events++;
      args[0] = v8_str("Promise rejected after resolved");
    }
    promise_reject_handler->Call(context->Global(), 3, args);
    return;
  }
}

void Print(const v8::FunctionCallbackInfo<v8::Value>& args) {
  CHECK_GE(args.Length(), 1);
  CHECK_LE(args.Length(), 2);
  auto* isolate = args.GetIsolate();
  Deno* d = static_cast<Deno*>(isolate->GetData(0));
  auto context = d->context.Get(d->isolate);
  v8::HandleScope handle_scope(isolate);
  v8::String::Utf8Value str(isolate, args[0]);
  bool is_err =
      args.Length() >= 2 ? args[1]->BooleanValue(context).ToChecked() : false;
  const char* cstr = ToCString(str);
  auto& stream = is_err ? std::cerr : std::cout;
  stream << cstr << std::endl;
}

static v8::Local<v8::Uint8Array> ImportBuf(Deno* d, deno_buf buf) {
  if (buf.alloc_ptr == nullptr) {
    // If alloc_ptr isn't set, we memcpy.
    // This is currently used for flatbuffers created in Rust.

    // To avoid excessively allocating new ArrayBuffers, we try to reuse a
    // single global ArrayBuffer. The caveat is that users must extract data
    // from it before the next tick. We only do this for ArrayBuffers less than
    // 1024 bytes.
    v8::Local<v8::ArrayBuffer> ab;
    void* data;
    if (buf.data_len > GLOBAL_IMPORT_BUF_SIZE) {
      // Simple case. We allocate a new ArrayBuffer for this.
      ab = v8::ArrayBuffer::New(d->isolate, buf.data_len);
      data = ab->GetContents().Data();
    } else {
      // Fast case. We reuse the global ArrayBuffer.
      if (d->global_import_buf.IsEmpty()) {
        // Lazily initialize it.
        DCHECK_EQ(d->global_import_buf_ptr, nullptr);
        ab = v8::ArrayBuffer::New(d->isolate, GLOBAL_IMPORT_BUF_SIZE);
        d->global_import_buf.Reset(d->isolate, ab);
        d->global_import_buf_ptr = ab->GetContents().Data();
      } else {
        DCHECK(d->global_import_buf_ptr);
        ab = d->global_import_buf.Get(d->isolate);
      }
      data = d->global_import_buf_ptr;
    }
    memcpy(data, buf.data_ptr, buf.data_len);
    auto view = v8::Uint8Array::New(ab, 0, buf.data_len);
    return view;
  } else {
    auto ab = v8::ArrayBuffer::New(
        d->isolate, reinterpret_cast<void*>(buf.alloc_ptr), buf.alloc_len,
        v8::ArrayBufferCreationMode::kInternalized);
    auto view =
        v8::Uint8Array::New(ab, buf.data_ptr - buf.alloc_ptr, buf.data_len);
    return view;
  }
}

static deno_buf GetContents(v8::Isolate* isolate,
                            v8::Local<v8::ArrayBufferView> view) {
  auto ab = view->Buffer();
  auto contents = ab->GetContents();
  deno_buf buf;
  buf.alloc_ptr = reinterpret_cast<uint8_t*>(contents.Data());
  buf.alloc_len = contents.ByteLength();
  buf.data_ptr = buf.alloc_ptr + view->ByteOffset();
  buf.data_len = view->ByteLength();
  return buf;
}

// Sets the recv callback.
void Recv(const v8::FunctionCallbackInfo<v8::Value>& args) {
  v8::Isolate* isolate = args.GetIsolate();
  Deno* d = reinterpret_cast<Deno*>(isolate->GetData(0));
  DCHECK_EQ(d->isolate, isolate);

  v8::HandleScope handle_scope(isolate);

  if (!d->recv.IsEmpty()) {
    isolate->ThrowException(v8_str("libdeno.recv already called."));
    return;
  }

  v8::Local<v8::Value> v = args[0];
  CHECK(v->IsFunction());
  v8::Local<v8::Function> func = v8::Local<v8::Function>::Cast(v);

  d->recv.Reset(isolate, func);
}

void Send(const v8::FunctionCallbackInfo<v8::Value>& args) {
  v8::Isolate* isolate = args.GetIsolate();
  Deno* d = static_cast<Deno*>(isolate->GetData(0));
  DCHECK_EQ(d->isolate, isolate);

  v8::Locker locker(d->isolate);
  v8::EscapableHandleScope handle_scope(isolate);

  CHECK_EQ(d->currentArgs, nullptr);  // libdeno.send re-entry forbidden.
  int32_t req_id = d->next_req_id++;

  v8::Local<v8::Value> control_v = args[0];
  CHECK(control_v->IsArrayBufferView());
  deno_buf control =
      GetContents(isolate, v8::Local<v8::ArrayBufferView>::Cast(control_v));
  deno_buf data = {nullptr, 0u, nullptr, 0u};
  v8::Local<v8::Value> data_v;
  if (args.Length() == 2) {
    if (args[1]->IsArrayBufferView()) {
      data_v = args[1];
      data = GetContents(isolate, v8::Local<v8::ArrayBufferView>::Cast(data_v));
    }
  } else {
    CHECK_EQ(args.Length(), 1);
  }

  DCHECK_EQ(d->currentArgs, nullptr);
  d->currentArgs = &args;

  d->cb(d->user_data, req_id, control, data);

  if (d->currentArgs == nullptr) {
    // This indicates that deno_repond() was called already.
  } else {
    // Asynchronous.
    d->currentArgs = nullptr;
    // If the data ArrayBuffer was given, we must maintain a strong reference
    // to it until deno_respond is called.
    if (!data_v.IsEmpty()) {
      AddDataRef(d, req_id, data_v);
    }
  }
}

// Sets the global error handler.
void SetGlobalErrorHandler(const v8::FunctionCallbackInfo<v8::Value>& args) {
  v8::Isolate* isolate = args.GetIsolate();
  Deno* d = reinterpret_cast<Deno*>(isolate->GetData(0));
  DCHECK_EQ(d->isolate, isolate);

  v8::HandleScope handle_scope(isolate);

  if (!d->global_error_handler.IsEmpty()) {
    isolate->ThrowException(
        v8_str("libdeno.setGlobalErrorHandler already called."));
    return;
  }

  v8::Local<v8::Value> v = args[0];
  CHECK(v->IsFunction());
  v8::Local<v8::Function> func = v8::Local<v8::Function>::Cast(v);

  d->global_error_handler.Reset(isolate, func);
}

// Sets the promise uncaught reject handler
void SetPromiseRejectHandler(const v8::FunctionCallbackInfo<v8::Value>& args) {
  v8::Isolate* isolate = args.GetIsolate();
  Deno* d = reinterpret_cast<Deno*>(isolate->GetData(0));
  DCHECK_EQ(d->isolate, isolate);

  v8::HandleScope handle_scope(isolate);

  if (!d->promise_reject_handler.IsEmpty()) {
    isolate->ThrowException(
        v8_str("libdeno.setPromiseRejectHandler already called."));
    return;
  }

  v8::Local<v8::Value> v = args[0];
  CHECK(v->IsFunction());
  v8::Local<v8::Function> func = v8::Local<v8::Function>::Cast(v);

  d->promise_reject_handler.Reset(isolate, func);
}

// Sets the promise uncaught reject handler
void SetPromiseErrorExaminer(const v8::FunctionCallbackInfo<v8::Value>& args) {
  v8::Isolate* isolate = args.GetIsolate();
  Deno* d = reinterpret_cast<Deno*>(isolate->GetData(0));
  DCHECK_EQ(d->isolate, isolate);

  v8::HandleScope handle_scope(isolate);

  if (!d->promise_error_examiner.IsEmpty()) {
    isolate->ThrowException(
        v8_str("libdeno.setPromiseErrorExaminer already called."));
    return;
  }

  v8::Local<v8::Value> v = args[0];
  CHECK(v->IsFunction());
  v8::Local<v8::Function> func = v8::Local<v8::Function>::Cast(v);

  d->promise_error_examiner.Reset(isolate, func);
}

bool ExecuteV8StringSource(v8::Local<v8::Context> context,
                           const char* js_filename,
                           v8::Local<v8::String> source) {
  auto* isolate = context->GetIsolate();
  v8::Isolate::Scope isolate_scope(isolate);
  v8::HandleScope handle_scope(isolate);

  v8::Context::Scope context_scope(context);

  v8::TryCatch try_catch(isolate);

  auto name = v8_str(js_filename);

  v8::ScriptOrigin origin(name);

  auto script = v8::Script::Compile(context, source, &origin);

  if (script.IsEmpty()) {
    DCHECK(try_catch.HasCaught());
    HandleException(context, try_catch.Exception());
    return false;
  }

  auto result = script.ToLocalChecked()->Run(context);

  if (result.IsEmpty()) {
    DCHECK(try_catch.HasCaught());
    HandleException(context, try_catch.Exception());
    return false;
  }

  return true;
}

bool Execute(v8::Local<v8::Context> context, const char* js_filename,
             const char* js_source) {
  auto* isolate = context->GetIsolate();
  v8::Isolate::Scope isolate_scope(isolate);
  v8::HandleScope handle_scope(isolate);
  auto source = v8_str(js_source);
  return ExecuteV8StringSource(context, js_filename, source);
}

void InitializeContext(v8::Isolate* isolate, v8::Local<v8::Context> context,
                       const char* js_filename, const std::string& js_source,
                       const std::string* source_map) {
  v8::HandleScope handle_scope(isolate);
  v8::Context::Scope context_scope(context);

  auto global = context->Global();

  auto deno_val = v8::Object::New(isolate);
  CHECK(global->Set(context, deno::v8_str("libdeno"), deno_val).FromJust());

  auto print_tmpl = v8::FunctionTemplate::New(isolate, Print);
  auto print_val = print_tmpl->GetFunction(context).ToLocalChecked();
  CHECK(deno_val->Set(context, deno::v8_str("print"), print_val).FromJust());

  auto recv_tmpl = v8::FunctionTemplate::New(isolate, Recv);
  auto recv_val = recv_tmpl->GetFunction(context).ToLocalChecked();
  CHECK(deno_val->Set(context, deno::v8_str("recv"), recv_val).FromJust());

  auto send_tmpl = v8::FunctionTemplate::New(isolate, Send);
  auto send_val = send_tmpl->GetFunction(context).ToLocalChecked();
  CHECK(deno_val->Set(context, deno::v8_str("send"), send_val).FromJust());

  auto set_global_error_handler_tmpl =
      v8::FunctionTemplate::New(isolate, SetGlobalErrorHandler);
  auto set_global_error_handler_val =
      set_global_error_handler_tmpl->GetFunction(context).ToLocalChecked();
  CHECK(deno_val
            ->Set(context, deno::v8_str("setGlobalErrorHandler"),
                  set_global_error_handler_val)
            .FromJust());

  auto set_promise_reject_handler_tmpl =
      v8::FunctionTemplate::New(isolate, SetPromiseRejectHandler);
  auto set_promise_reject_handler_val =
      set_promise_reject_handler_tmpl->GetFunction(context).ToLocalChecked();
  CHECK(deno_val
            ->Set(context, deno::v8_str("setPromiseRejectHandler"),
                  set_promise_reject_handler_val)
            .FromJust());

  auto set_promise_error_examiner_tmpl =
      v8::FunctionTemplate::New(isolate, SetPromiseErrorExaminer);
  auto set_promise_error_examiner_val =
      set_promise_error_examiner_tmpl->GetFunction(context).ToLocalChecked();
  CHECK(deno_val
            ->Set(context, deno::v8_str("setPromiseErrorExaminer"),
                  set_promise_error_examiner_val)
            .FromJust());

  {
    auto source = deno::v8_str(js_source.c_str());
    CHECK(
        deno_val->Set(context, deno::v8_str("mainSource"), source).FromJust());

    bool r = deno::ExecuteV8StringSource(context, js_filename, source);
    CHECK(r);

    if (source_map != nullptr) {
      CHECK_GT(source_map->length(), 1u);
      v8::TryCatch try_catch(isolate);
      v8::ScriptOrigin origin(v8_str("set_source_map.js"));
      std::string source_map_parens = "(" + *source_map + ")";
      auto source_map_v8_str = deno::v8_str(source_map_parens.c_str());
      auto script = v8::Script::Compile(context, source_map_v8_str, &origin);
      if (script.IsEmpty()) {
        DCHECK(try_catch.HasCaught());
        HandleException(context, try_catch.Exception());
        return;
      }
      auto source_map_obj = script.ToLocalChecked()->Run(context);
      if (source_map_obj.IsEmpty()) {
        DCHECK(try_catch.HasCaught());
        HandleException(context, try_catch.Exception());
        return;
      }
      CHECK(deno_val
                ->Set(context, deno::v8_str("mainSourceMap"),
                      source_map_obj.ToLocalChecked())
                .FromJust());
    }
  }
}

void AddIsolate(Deno* d, v8::Isolate* isolate) {
  d->pending_promise_events = 0;
  d->next_req_id = 0;
  d->global_import_buf_ptr = nullptr;
  d->isolate = isolate;
  // Leaving this code here because it will probably be useful later on, but
  // disabling it now as I haven't got tests for the desired behavior.
  // d->isolate->SetCaptureStackTraceForUncaughtExceptions(true);
  // d->isolate->SetAbortOnUncaughtExceptionCallback(AbortOnUncaughtExceptionCallback);
  // d->isolate->AddMessageListener(MessageCallback2);
  // d->isolate->SetFatalErrorHandler(FatalErrorCallback2);
  d->isolate->SetPromiseRejectCallback(deno::PromiseRejectCallback);
  d->isolate->SetData(0, d);
}

class UserDataScope {
  Deno* deno;
  void* prev_data;
  void* data;  // Not necessary; only for sanity checking.

 public:
  UserDataScope(Deno* deno_, void* data_) : deno(deno_), data(data_) {
    CHECK(deno->user_data == nullptr || deno->user_data == data_);
    prev_data = deno->user_data;
    deno->user_data = data;
  }

  ~UserDataScope() {
    CHECK(deno->user_data == data);
    deno->user_data = prev_data;
  }
};

}  // namespace deno

extern "C" {

void deno_init() {
  // v8::V8::InitializeICUDefaultLocation(argv[0]);
  // v8::V8::InitializeExternalStartupData(argv[0]);
  auto* p = v8::platform::CreateDefaultPlatform();
  v8::V8::InitializePlatform(p);
  v8::V8::Initialize();
}

const char* deno_v8_version() { return v8::V8::GetVersion(); }

void deno_set_v8_flags(int* argc, char** argv) {
  v8::V8::SetFlagsFromCommandLine(argc, argv, true);
}

const char* deno_last_exception(Deno* d) { return d->last_exception.c_str(); }

int deno_execute(Deno* d, void* user_data, const char* js_filename,
                 const char* js_source) {
  deno::UserDataScope user_data_scope(d, user_data);
  auto* isolate = d->isolate;
  v8::Locker locker(isolate);
  v8::Isolate::Scope isolate_scope(isolate);
  v8::HandleScope handle_scope(isolate);
  auto context = d->context.Get(d->isolate);
  return deno::Execute(context, js_filename, js_source) ? 1 : 0;
}

int deno_respond(Deno* d, void* user_data, int32_t req_id, deno_buf buf) {
  if (d->currentArgs != nullptr) {
    // Synchronous response.
    auto ab = deno::ImportBuf(d, buf);
    d->currentArgs->GetReturnValue().Set(ab);
    d->currentArgs = nullptr;
    return 0;
  }

  // Asynchronous response.
  deno::UserDataScope user_data_scope(d, user_data);
  v8::Locker locker(d->isolate);
  v8::Isolate::Scope isolate_scope(d->isolate);
  v8::HandleScope handle_scope(d->isolate);

  auto context = d->context.Get(d->isolate);
  v8::Context::Scope context_scope(context);

  v8::TryCatch try_catch(d->isolate);

  deno::DeleteDataRef(d, req_id);

  auto recv = d->recv.Get(d->isolate);
  if (recv.IsEmpty()) {
    d->last_exception = "libdeno.recv has not been called.";
    return 1;
  }

  v8::Local<v8::Value> args[1];
  args[0] = deno::ImportBuf(d, buf);
  recv->Call(context->Global(), 1, args);

  if (try_catch.HasCaught()) {
    deno::HandleException(context, try_catch.Exception());
    return 1;
  }

  return 0;
}

void deno_check_promise_errors(Deno* d) {
  if (d->pending_promise_events > 0) {
    auto* isolate = d->isolate;
    v8::Locker locker(isolate);
    v8::Isolate::Scope isolate_scope(isolate);
    v8::HandleScope handle_scope(isolate);

    auto context = d->context.Get(d->isolate);
    v8::Context::Scope context_scope(context);

    v8::TryCatch try_catch(d->isolate);
    auto promise_error_examiner = d->promise_error_examiner.Get(d->isolate);
    if (promise_error_examiner.IsEmpty()) {
      d->last_exception =
          "libdeno.setPromiseErrorExaminer has not been called.";
      return;
    }
    v8::Local<v8::Value> args[0];
    auto result = promise_error_examiner->Call(context->Global(), 0, args);
    if (try_catch.HasCaught()) {
      deno::HandleException(context, try_catch.Exception());
    }
    d->pending_promise_events = 0;  // reset
    if (!result->BooleanValue(context).FromJust()) {
      // Has uncaught promise reject error, exiting...
      exit(1);
    }
  }
}

void deno_delete(Deno* d) {
  d->isolate->Dispose();
  delete d;
}

void deno_terminate_execution(Deno* d) { d->isolate->TerminateExecution(); }

}  // extern "C"