summaryrefslogtreecommitdiff
path: root/src/main.cc
blob: a801bc29210251ab24c1b497a2cb7c54f984c623 (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
// Copyright 2018 Ryan Dahl <ry@tinyclouds.org>
// All rights reserved. MIT License.
#include <stdio.h>
#include <stdlib.h>
#include <string>

#ifdef _WIN32
#include <direct.h>
#else
#include <unistd.h>
#endif

#include "deno.h"
#include "flatbuffers/flatbuffers.h"
#include "src/handlers.h"
#include "src/msg_generated.h"
#include "third_party/v8/src/base/logging.h"

namespace deno {

static char** global_argv;
static int global_argc;

// Sends StartRes message
void HandleStart(Deno* d, uint32_t cmd_id) {
  flatbuffers::FlatBufferBuilder builder;

  char cwdbuf[1024];
  // TODO(piscisaureus): support unicode on windows.
  getcwd(cwdbuf, sizeof(cwdbuf));
  auto start_cwd = builder.CreateString(cwdbuf);

  std::vector<flatbuffers::Offset<flatbuffers::String>> args;
  for (int i = 0; i < global_argc; ++i) {
    args.push_back(builder.CreateString(global_argv[i]));
  }

  auto start_argv = builder.CreateVector(args);
  auto start_msg = CreateStartRes(builder, start_cwd, start_argv);
  auto base = CreateBase(builder, cmd_id, 0, Any_StartRes, start_msg.Union());
  builder.Finish(base);
  deno_buf bufout{reinterpret_cast<const char*>(builder.GetBufferPointer()),
                  builder.GetSize()};
  deno_set_response(d, bufout);
}

void HandleCodeFetch(Deno* d, uint32_t cmd_id, const CodeFetch* msg) {
  auto module_specifier = msg->module_specifier()->c_str();
  auto containing_file = msg->containing_file()->c_str();
  printf("HandleCodeFetch module_specifier = %s containing_file = %s\n",
         module_specifier, containing_file);
  // Call into rust.
  handle_code_fetch(cmd_id, module_specifier, containing_file);
}

void MessagesFromJS(Deno* d, deno_buf buf) {
  auto data = reinterpret_cast<const uint8_t*>(buf.data);
  flatbuffers::Verifier verifier(data, buf.len);
  DCHECK(verifier.VerifyBuffer<Base>());

  auto base = flatbuffers::GetRoot<Base>(buf.data);
  auto cmd_id = base->cmdId();
  auto msg_type = base->msg_type();
  const char* msg_type_name = EnumNamesAny()[msg_type];
  printf("MessagesFromJS cmd_id = %d, msg_type = %d, msg_type_name = %s\n",
         cmd_id, msg_type, msg_type_name);
  switch (msg_type) {
    case Any_Start:
      HandleStart(d, base->cmdId());
      break;

    case Any_CodeFetch:
      HandleCodeFetch(d, base->cmdId(), base->msg_as_CodeFetch());
      break;

    case Any_NONE:
      CHECK(false && "Got message with msg_type == Any_NONE");
      break;

    default:
      printf("Unhandled message %s\n", msg_type_name);
      CHECK(false && "Unhandled message");
      break;
  }
}

int deno_main(int argc, char** argv) {
  deno_init();

  deno_set_flags(&argc, argv);
  global_argv = argv;
  global_argc = argc;

  Deno* d = deno_new(NULL, MessagesFromJS);
  bool r = deno_execute(d, "deno_main.js", "denoMain();");
  if (!r) {
    printf("%s\n", deno_last_exception(d));
    exit(1);
  }
  deno_delete(d);
  return 0;
}

}  // namespace deno

int main(int argc, char** argv) { return deno::deno_main(argc, argv); }