Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // V8TestApplication1.cpp : This file contains the 'main' function. Program execution begins and ends there.
- //
- #include "pch.h"
- #include <iostream>
- #pragma comment(lib, "v8_base_0.lib")
- #pragma comment(lib, "v8_base_1.lib")
- #pragma comment(lib, "v8_libbase")
- #pragma comment(lib, "v8_external_snapshot")
- #pragma comment(lib, "v8_libplatform")
- #pragma comment(lib, "v8_libsampler")
- #pragma comment(lib, "icuuc.lib")
- #pragma comment(lib, "icui18n.lib")
- // Copyright 2015 the V8 project authors. All rights reserved.
- // Use of this source code is governed by a BSD-style license that can be
- // found in the LICENSE file.
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include "libplatform/libplatform.h"
- #include "v8.h"
- int main(int argc, char* argv[]) {
- // Initialize V8.
- v8::V8::InitializeICUDefaultLocation(argv[0]);
- v8::V8::InitializeExternalStartupData(argv[0]);
- std::unique_ptr<v8::Platform> platform = v8::platform::NewDefaultPlatform();
- v8::V8::InitializePlatform(platform.get());
- v8::V8::Initialize();
- // Create a new Isolate and make it the current one.
- v8::Isolate::CreateParams create_params;
- create_params.array_buffer_allocator =
- v8::ArrayBuffer::Allocator::NewDefaultAllocator();
- v8::Isolate* isolate = v8::Isolate::New(create_params);
- {
- v8::Isolate::Scope isolate_scope(isolate);
- // Create a stack-allocated handle scope.
- v8::HandleScope handle_scope(isolate);
- // Create a new context.
- v8::Local<v8::Context> context = v8::Context::New(isolate);
- // Enter the context for compiling and running the hello world script.
- v8::Context::Scope context_scope(context);
- {
- // Create a string containing the JavaScript source code.
- v8::Local<v8::String> source =
- v8::String::NewFromUtf8(isolate, "var a=5; function b() { return 7; }",
- v8::NewStringType::kNormal)
- .ToLocalChecked();
- // Compile the source code.
- v8::Local<v8::Script> script =
- v8::Script::Compile(context, source).ToLocalChecked();
- // Run the script to get the result.
- v8::Local<v8::Value> result = script->Run(context).ToLocalChecked();
- // ignoring the result...
- // print how many property names there are, and what those names are...
- v8::Local<v8::Array> names = context->Global()->GetPropertyNames();
- printf("%d\n", names->Length());
- for (unsigned int i = 0; i < names->Length(); ++i) {
- v8::Local<v8::Value> name = names->Get(i);
- v8::String::Utf8Value utf8(isolate, name);
- printf("%s\n", *utf8);
- }
- // now let's call b(), with this==null, and print its result (which should be 7)
- v8::Local<v8::String> name = v8::String::NewFromUtf8(isolate, "b", v8::NewStringType::kInternalized).ToLocalChecked();
- v8::Local<v8::Value> func=context->Global()->GetRealNamedProperty(context, name).ToLocalChecked();
- v8::Local<v8::Value> jsnull = v8::Null(isolate);
- v8::Local<v8::Value> result2 = func->ToObject(isolate)->CallAsFunction(context, jsnull, 0, &jsnull).ToLocalChecked();
- v8::String::Utf8Value utf8_r2(isolate, result2);
- printf("%s\n", *utf8_r2);
- }
- }
- // Dispose the isolate and tear down V8.
- isolate->Dispose();
- v8::V8::Dispose();
- v8::V8::ShutdownPlatform();
- delete create_params.array_buffer_allocator;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement