2018-07-23 14:46:30 -04:00
|
|
|
// Copyright 2018 the Deno authors. All rights reserved. MIT license.
|
2018-08-10 13:41:12 -04:00
|
|
|
// This file is used to load the bundle at start for deno_ns.
|
2018-07-12 15:06:36 -04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include "third_party/v8/include/v8.h"
|
|
|
|
#include "third_party/v8/src/base/logging.h"
|
|
|
|
|
|
|
|
#include "deno.h"
|
|
|
|
#include "file_util.h"
|
|
|
|
#include "internal.h"
|
|
|
|
|
|
|
|
namespace deno {
|
|
|
|
|
|
|
|
Deno* NewFromFileSystem(void* data, deno_recv_cb cb) {
|
2018-08-25 19:38:23 -04:00
|
|
|
std::string exe_path;
|
|
|
|
CHECK(deno::ExePath(&exe_path));
|
|
|
|
std::string exe_dir = deno::Dirname(exe_path); // Always ends with a slash.
|
|
|
|
|
|
|
|
std::string js_source_path = exe_dir + BUNDLE_LOCATION;
|
2018-07-12 15:06:36 -04:00
|
|
|
std::string js_source;
|
2018-08-25 19:38:23 -04:00
|
|
|
CHECK(deno::ReadFileToString(js_source_path.c_str(), &js_source));
|
2018-07-12 15:06:36 -04:00
|
|
|
|
2018-08-25 19:38:23 -04:00
|
|
|
std::string js_source_map_path = exe_dir + BUNDLE_MAP_LOCATION;
|
2018-08-10 13:41:12 -04:00
|
|
|
std::string js_source_map;
|
2018-08-25 19:38:23 -04:00
|
|
|
CHECK(deno::ReadFileToString(js_source_map_path.c_str(), &js_source_map));
|
2018-08-10 13:41:12 -04:00
|
|
|
|
2018-07-12 15:06:36 -04:00
|
|
|
Deno* d = new Deno;
|
|
|
|
d->currentArgs = nullptr;
|
|
|
|
d->cb = cb;
|
|
|
|
d->data = data;
|
|
|
|
v8::Isolate::CreateParams params;
|
|
|
|
params.array_buffer_allocator =
|
|
|
|
v8::ArrayBuffer::Allocator::NewDefaultAllocator();
|
|
|
|
v8::Isolate* isolate = v8::Isolate::New(params);
|
|
|
|
AddIsolate(d, isolate);
|
|
|
|
|
|
|
|
v8::Locker locker(isolate);
|
|
|
|
v8::Isolate::Scope isolate_scope(isolate);
|
|
|
|
{
|
|
|
|
v8::HandleScope handle_scope(isolate);
|
|
|
|
auto context = v8::Context::New(isolate);
|
2018-08-25 19:38:23 -04:00
|
|
|
// For source maps to work, the bundle location that is passed to
|
|
|
|
// InitializeContext must be a relative path.
|
|
|
|
InitializeContext(isolate, context, BUNDLE_LOCATION, js_source,
|
2018-08-10 13:41:12 -04:00
|
|
|
&js_source_map);
|
2018-07-12 15:06:36 -04:00
|
|
|
d->context.Reset(d->isolate, context);
|
|
|
|
}
|
|
|
|
|
|
|
|
return d;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace deno
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
Deno* deno_new(void* data, deno_recv_cb cb) {
|
|
|
|
return deno::NewFromFileSystem(data, cb);
|
|
|
|
}
|
|
|
|
}
|