Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.23 KB | None | 0 0
  1. // V8TestApplication1.cpp : This file contains the 'main' function. Program execution begins and ends there.
  2. //
  3.  
  4. #include "pch.h"
  5. #include <iostream>
  6.  
  7. #pragma comment(lib, "v8_base_0.lib")
  8. #pragma comment(lib, "v8_base_1.lib")
  9. #pragma comment(lib, "v8_libbase")
  10. #pragma comment(lib, "v8_external_snapshot")
  11. #pragma comment(lib, "v8_libplatform")
  12. #pragma comment(lib, "v8_libsampler")
  13. #pragma comment(lib, "icuuc.lib")
  14. #pragma comment(lib, "icui18n.lib")
  15.  
  16.  
  17. // Copyright 2015 the V8 project authors. All rights reserved.
  18. // Use of this source code is governed by a BSD-style license that can be
  19. // found in the LICENSE file.
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include "libplatform/libplatform.h"
  24. #include "v8.h"
  25. int main(int argc, char* argv[]) {
  26. // Initialize V8.
  27. v8::V8::InitializeICUDefaultLocation(argv[0]);
  28. v8::V8::InitializeExternalStartupData(argv[0]);
  29. std::unique_ptr<v8::Platform> platform = v8::platform::NewDefaultPlatform();
  30. v8::V8::InitializePlatform(platform.get());
  31. v8::V8::Initialize();
  32. // Create a new Isolate and make it the current one.
  33. v8::Isolate::CreateParams create_params;
  34. create_params.array_buffer_allocator =
  35. v8::ArrayBuffer::Allocator::NewDefaultAllocator();
  36. v8::Isolate* isolate = v8::Isolate::New(create_params);
  37. {
  38. v8::Isolate::Scope isolate_scope(isolate);
  39. // Create a stack-allocated handle scope.
  40. v8::HandleScope handle_scope(isolate);
  41. // Create a new context.
  42. v8::Local<v8::Context> context = v8::Context::New(isolate);
  43. // Enter the context for compiling and running the hello world script.
  44. v8::Context::Scope context_scope(context);
  45. {
  46. // Create a string containing the JavaScript source code.
  47. v8::Local<v8::String> source =
  48. v8::String::NewFromUtf8(isolate, "var a=5; function b() { return 7; }",
  49. v8::NewStringType::kNormal)
  50. .ToLocalChecked();
  51. // Compile the source code.
  52. v8::Local<v8::Script> script =
  53. v8::Script::Compile(context, source).ToLocalChecked();
  54. // Run the script to get the result.
  55. v8::Local<v8::Value> result = script->Run(context).ToLocalChecked();
  56. // ignoring the result...
  57.  
  58. // print how many property names there are, and what those names are...
  59. v8::Local<v8::Array> names = context->Global()->GetPropertyNames();
  60. printf("%d\n", names->Length());
  61. for (unsigned int i = 0; i < names->Length(); ++i) {
  62. v8::Local<v8::Value> name = names->Get(i);
  63. v8::String::Utf8Value utf8(isolate, name);
  64. printf("%s\n", *utf8);
  65. }
  66.  
  67. // now let's call b(), with this==null, and print its result (which should be 7)
  68. v8::Local<v8::String> name = v8::String::NewFromUtf8(isolate, "b", v8::NewStringType::kInternalized).ToLocalChecked();
  69. v8::Local<v8::Value> func=context->Global()->GetRealNamedProperty(context, name).ToLocalChecked();
  70. v8::Local<v8::Value> jsnull = v8::Null(isolate);
  71. v8::Local<v8::Value> result2 = func->ToObject(isolate)->CallAsFunction(context, jsnull, 0, &jsnull).ToLocalChecked();
  72. v8::String::Utf8Value utf8_r2(isolate, result2);
  73. printf("%s\n", *utf8_r2);
  74.  
  75. }
  76. }
  77. // Dispose the isolate and tear down V8.
  78. isolate->Dispose();
  79. v8::V8::Dispose();
  80. v8::V8::ShutdownPlatform();
  81. delete create_params.array_buffer_allocator;
  82. return 0;
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement