v8eval 0.2.3 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -55,20 +55,3 @@ RSpec.describe V8Eval, '#multithreading' do
55
55
  end
56
56
  end
57
57
  end
58
-
59
- RSpec.describe V8Eval, '#test_debugger' do
60
- context 'with debugging functionality' do
61
- it 'should enable and disable debugger properly' do
62
- v8 = V8Eval::V8.new
63
- expect { v8.enable_debugger(0.1) }.to raise_exception(TypeError)
64
- expect { v8.enable_debugger(-1) }.to raise_exception(V8Eval::V8Error)
65
-
66
- port = 12_345
67
- expect(v8.enable_debugger(port)).to be nil
68
- expect { v8.enable_debugger(port) }.to raise_exception(V8Eval::V8Error)
69
- expect(v8.disable_debugger).to be nil
70
- expect(v8.enable_debugger(port)).to be nil
71
- expect(v8.disable_debugger).to be nil
72
- end
73
- end
74
- end
@@ -1,8 +1,8 @@
1
1
  #include "v8eval.h"
2
- #include "dbgsrv.h"
3
2
 
4
- #include <stdlib.h>
5
- #include <string.h>
3
+ #include <cassert>
4
+ #include <cstdlib>
5
+ #include <cstring>
6
6
 
7
7
  #include "libplatform/libplatform.h"
8
8
 
@@ -10,15 +10,15 @@ namespace v8eval {
10
10
 
11
11
  static v8::Platform* platform = nullptr;
12
12
 
13
+ void set_flags(const std::string& flags) {
14
+ v8::V8::SetFlagsFromString(flags.c_str(), static_cast<int>(flags.length()));
15
+ }
16
+
13
17
  bool initialize() {
14
18
  if (platform) {
15
19
  return false;
16
20
  }
17
21
 
18
- if (!v8::V8::InitializeICU()) {
19
- return false;
20
- }
21
-
22
22
  platform = v8::platform::CreateDefaultPlatform();
23
23
  v8::V8::InitializePlatform(platform);
24
24
 
@@ -39,39 +39,12 @@ bool dispose() {
39
39
  return true;
40
40
  }
41
41
 
42
- class ArrayBufferAllocator : public v8::ArrayBuffer::Allocator {
43
- public:
44
- virtual void* Allocate(size_t length) {
45
- void* data = AllocateUninitialized(length);
46
- return data == NULL ? data : memset(data, 0, length);
47
- }
48
-
49
- virtual void* AllocateUninitialized(size_t length) {
50
- return malloc(length);
51
- }
52
-
53
- virtual void Free(void* data, size_t) {
54
- free(data);
55
- }
56
- };
57
-
58
- static ArrayBufferAllocator allocator;
59
-
60
42
  _V8::_V8() {
61
43
  v8::Isolate::CreateParams create_params;
62
- create_params.array_buffer_allocator = &allocator;
44
+ create_params.array_buffer_allocator =
45
+ v8::ArrayBuffer::Allocator::NewDefaultAllocator();
63
46
  isolate_ = v8::Isolate::New(create_params);
64
47
 
65
- dbg_server_ = nullptr;
66
- dbg_isolate_ = nullptr;
67
- callback_ = nullptr;
68
- callback_opq_ = nullptr;
69
-
70
- // Use Isolate's local storage to store a pointer to the associated
71
- // V8 instance. This is retrieved in the V8 debugger's callback, as
72
- // the instance pointer is the only context we get from the caller.
73
- isolate_->SetData(0, this);
74
-
75
48
  v8::Locker locker(isolate_);
76
49
 
77
50
  v8::Isolate::Scope isolate_scope(isolate_);
@@ -80,20 +53,20 @@ _V8::_V8() {
80
53
  }
81
54
 
82
55
  _V8::~_V8() {
83
- if (dbg_server_) {
84
- delete dbg_server_;
85
- }
86
-
87
- context_.Reset();
88
56
  isolate_->Dispose();
89
57
  }
90
58
 
91
- v8::Local<v8::Context> _V8::new_context() {
92
- if (context_.IsEmpty()) {
59
+ v8::Local<v8::Context> _V8::context() {
60
+ assert(context_.IsEmpty());
61
+ return v8::Local<v8::Context>::New(isolate_, context_);
62
+ }
63
+
64
+ v8::Local<v8::Context> _V8::new_context(v8::Local<v8::ObjectTemplate> global_tmpl, v8::Local<v8::Value> global_obj) {
65
+ if (global_tmpl.IsEmpty() && global_obj.IsEmpty()) {
93
66
  v8::Local<v8::ObjectTemplate> global = v8::ObjectTemplate::New(isolate_);
94
67
  return v8::Context::New(isolate_, nullptr, global);
95
68
  } else {
96
- return v8::Local<v8::Context>::New(isolate_, context_);
69
+ return v8::Context::New(isolate_, nullptr, global_tmpl, global_obj);
97
70
  }
98
71
  }
99
72
 
@@ -101,14 +74,14 @@ v8::Local<v8::String> _V8::new_string(const char* str) {
101
74
  return v8::String::NewFromUtf8(isolate_, str ? str : "", v8::NewStringType::kNormal).ToLocalChecked();
102
75
  }
103
76
 
104
- static std::string to_std_string(v8::Local<v8::Value> value) {
105
- v8::String::Utf8Value str(value);
77
+ std::string _V8::to_std_string(v8::Local<v8::Value> value) {
78
+ v8::String::Utf8Value str(isolate_, value);
106
79
  return *str ? *str : "Error: Cannot convert to string";
107
80
  }
108
81
 
109
82
  v8::Local<v8::Value> _V8::json_parse(v8::Local<v8::Context> context, v8::Local<v8::String> str) {
110
83
  v8::Local<v8::Object> global = context->Global();
111
- v8::Local<v8::Object> json = global->Get(context, new_string("JSON")).ToLocalChecked()->ToObject();
84
+ v8::Local<v8::Object> json = global->Get(context, new_string("JSON")).ToLocalChecked()->ToObject(isolate_);
112
85
  v8::Local<v8::Function> parse = v8::Local<v8::Function>::Cast(json->Get(context, new_string("parse")).ToLocalChecked());
113
86
 
114
87
  v8::Local<v8::Value> result;
@@ -122,14 +95,14 @@ v8::Local<v8::Value> _V8::json_parse(v8::Local<v8::Context> context, v8::Local<v
122
95
 
123
96
  v8::Local<v8::String> _V8::json_stringify(v8::Local<v8::Context> context, v8::Local<v8::Value> value) {
124
97
  v8::Local<v8::Object> global = context->Global();
125
- v8::Local<v8::Object> json = global->Get(context, new_string("JSON")).ToLocalChecked()->ToObject();
98
+ v8::Local<v8::Object> json = global->Get(context, new_string("JSON")).ToLocalChecked()->ToObject(isolate_);
126
99
  v8::Local<v8::Function> stringify = v8::Local<v8::Function>::Cast(json->Get(context, new_string("stringify")).ToLocalChecked());
127
100
 
128
101
  v8::Local<v8::Value> result;
129
102
  if (!stringify->Call(context, json, 1, &value).ToLocal(&result)) {
130
103
  return new_string("");
131
104
  } else {
132
- return result->ToString();
105
+ return result->ToString(isolate_);
133
106
  }
134
107
  }
135
108
 
@@ -139,7 +112,7 @@ std::string _V8::eval(const std::string& src) {
139
112
  v8::Isolate::Scope isolate_scope(isolate_);
140
113
  v8::HandleScope handle_scope(isolate_);
141
114
 
142
- v8::Local<v8::Context> context = new_context();
115
+ v8::Local<v8::Context> context = this->context();
143
116
  v8::Context::Scope context_scope(context);
144
117
 
145
118
  v8::TryCatch try_catch(isolate_);
@@ -155,7 +128,12 @@ std::string _V8::eval(const std::string& src) {
155
128
  } else {
156
129
  v8::Local<v8::Value> result;
157
130
  if (!script->Run(context).ToLocal(&result)) {
158
- return to_std_string(try_catch.Exception());
131
+ v8::Local<v8::Value> stack;
132
+ if (!try_catch.StackTrace(context).ToLocal(&stack)) {
133
+ return to_std_string(try_catch.Exception());
134
+ } else {
135
+ return to_std_string(stack);
136
+ }
159
137
  } else {
160
138
  return to_std_string(json_stringify(context, result));
161
139
  }
@@ -168,7 +146,7 @@ std::string _V8::call(const std::string& func, const std::string& args) {
168
146
  v8::Isolate::Scope isolate_scope(isolate_);
169
147
  v8::HandleScope handle_scope(isolate_);
170
148
 
171
- v8::Local<v8::Context> context = new_context();
149
+ v8::Local<v8::Context> context = this->context();
172
150
  v8::Context::Scope context_scope(context);
173
151
 
174
152
  v8::TryCatch try_catch(isolate_);
@@ -196,93 +174,36 @@ std::string _V8::call(const std::string& func, const std::string& args) {
196
174
  }
197
175
  }
198
176
 
199
- bool _V8::enable_debugger(int port) {
200
- if (dbg_server_) {
201
- return false;
202
- }
177
+ void Heap(const v8::FunctionCallbackInfo<v8::Value>& args) {
178
+ v8::Isolate* isolate = args.GetIsolate();
203
179
 
204
- dbg_server_ = new DbgSrv(*this);
205
- if (!dbg_server_->start(port)) {
206
- delete dbg_server_;
207
- dbg_server_ = nullptr;
208
- return false;
209
- }
210
-
211
- return true;
212
- }
213
-
214
- void _V8::disable_debugger() {
215
- if (dbg_server_) {
216
- delete dbg_server_;
217
- dbg_server_ = nullptr;
218
- }
219
- }
180
+ v8::HeapStatistics s;
181
+ isolate->GetHeapStatistics(&s);
220
182
 
221
- void _V8::debugger_message_handler(const v8::Debug::Message& message) {
222
- v8::Isolate* isolate = message.GetIsolate();
223
- _V8 *_v8 = (v8eval::_V8*)isolate->GetData(0);
224
- std::string string = *v8::String::Utf8Value(message.GetJSON());
183
+ v8::Local<v8::Object> obj = v8::Object::New(isolate);
184
+ obj->Set(v8::String::NewFromUtf8(isolate, "totalHeapSize"), v8::Number::New(isolate, s.total_heap_size()));
185
+ obj->Set(v8::String::NewFromUtf8(isolate, "totalHeapSizeExecutable"), v8::Number::New(isolate, s.total_heap_size_executable()));
186
+ obj->Set(v8::String::NewFromUtf8(isolate, "totalPhysicalSize"), v8::Number::New(isolate, s.total_physical_size()));
187
+ obj->Set(v8::String::NewFromUtf8(isolate, "totalAvailableSize"), v8::Number::New(isolate, s.total_available_size()));
188
+ obj->Set(v8::String::NewFromUtf8(isolate, "usedHeapSize"), v8::Number::New(isolate, s.used_heap_size()));
189
+ obj->Set(v8::String::NewFromUtf8(isolate, "heapSizeLimit"), v8::Number::New(isolate, s.heap_size_limit()));
190
+ obj->Set(v8::String::NewFromUtf8(isolate, "mallocedMemory"), v8::Number::New(isolate, s.malloced_memory()));
191
+ obj->Set(v8::String::NewFromUtf8(isolate, "peakMallocedMemory"), v8::Number::New(isolate, s.peak_malloced_memory()));
192
+ obj->Set(v8::String::NewFromUtf8(isolate, "doesZapGarbage"), v8::Number::New(isolate, s.does_zap_garbage()));
225
193
 
226
- if (_v8->callback_) {
227
- _v8->callback_(string, _v8->callback_opq_);
228
- }
194
+ args.GetReturnValue().Set(obj);
229
195
  }
230
196
 
231
- bool _V8::debugger_init(debugger_cb cb, void *cbopq) {
232
- v8::Isolate::CreateParams create_params;
233
- create_params.array_buffer_allocator = &allocator;
234
- dbg_isolate_ = v8::Isolate::New(create_params);
235
-
236
- if (callback_) {
237
- return false;
238
- }
239
-
240
- callback_ = cb;
241
- callback_opq_ = cbopq;
242
-
243
- // Set Debuger callback.
197
+ void _V8::enable_heap_report() {
244
198
  v8::Locker locker(isolate_);
245
- v8::Isolate::Scope isolate_scope(isolate_);
246
- v8::HandleScope handle_scope(isolate_);
247
- v8::Debug::SetMessageHandler(debugger_message_handler);
248
199
 
249
- return true;
250
- }
251
-
252
- void _V8::debugger_process() {
253
- // Process debug messages on behalf of the V8 instance.
254
- v8::Locker locker(isolate_);
255
200
  v8::Isolate::Scope isolate_scope(isolate_);
256
201
  v8::HandleScope handle_scope(isolate_);
257
202
 
258
- v8::Debug::ProcessDebugMessages();
259
- }
260
-
261
- bool _V8::debugger_send(const std::string& cmd) {
262
- v8::Locker locker(dbg_isolate_);
263
- v8::Isolate::Scope isolate_scope(dbg_isolate_);
264
- v8::HandleScope handle_scope(dbg_isolate_);
265
-
266
- if (!callback_) {
267
- return false;
268
- }
269
-
270
- v8::Local<v8::String> vstr = v8::String::NewFromUtf8(dbg_isolate_, cmd.c_str(), v8::NewStringType::kNormal).ToLocalChecked();
271
- v8::String::Value v(vstr);
272
- v8::Debug::SendCommand(isolate_, *v, v.length());
273
- return true;
274
- }
275
-
276
- void _V8::debugger_stop() {
277
- v8::Locker locker(isolate_);
278
- v8::Isolate::Scope isolate_scope(isolate_);
279
- v8::HandleScope handle_scope(isolate_);
280
- v8::Debug::SetMessageHandler(nullptr);
203
+ v8::Local<v8::Context> context = this->context();
204
+ v8::Context::Scope context_scope(context);
281
205
 
282
- callback_ = nullptr;
283
- callback_opq_ = nullptr;
284
- dbg_isolate_->Dispose();
285
- dbg_isolate_ = nullptr;
206
+ context->Global()->Set(new_string("heap"), v8::FunctionTemplate::New(isolate_, Heap)->GetFunction());
286
207
  }
287
208
 
288
209
  } // namespace v8eval
@@ -4,14 +4,14 @@
4
4
  #include <string>
5
5
 
6
6
  #include "v8.h"
7
- #include "v8-debug.h"
8
7
 
9
8
  /// \file
10
9
  namespace v8eval {
11
10
 
12
- class DbgSrv;
13
-
14
- typedef void (*debugger_cb)(std::string&, void *opq);
11
+ /// \brief Set the given V8 flags
12
+ ///
13
+ /// This method sets the given V8 flags.
14
+ void set_flags(const std::string& flags);
15
15
 
16
16
  /// \brief Initialize the V8 runtime environment
17
17
  /// \return success or not as boolean
@@ -53,45 +53,21 @@ class _V8 {
53
53
  /// If some JavaScript exception happens in runtime, the exception message is returned.
54
54
  std::string call(const std::string& func, const std::string& args);
55
55
 
56
- /// \brief Start a debug server associated with the V8 instance
57
- /// \param port The TCP/IP port the debugger will listen, at localhost
58
- /// \return success or not as boolean
59
- ///
60
- /// After the debugger is successfully started, it will be possible to
61
- /// send commands and receive events at the specified port. When the
62
- /// debugger is started, the Javascript's "debugger" statement will
63
- /// cause the V8 instance to halt and wait for instructions through
64
- /// the debugger port.
65
- bool enable_debugger(int port);
66
-
67
- /// \brief Stop the debug server, if running.
68
- ///
69
- /// The debug server, if currently running, will be stopped, causing
70
- /// connections to remote debuggers to be dropped.
71
- void disable_debugger();
56
+ protected:
57
+ void enable_heap_report();
72
58
 
73
- private:
74
- static void debugger_message_handler(const v8::Debug::Message& message);
75
- v8::Local<v8::Context> new_context();
59
+ v8::Local<v8::Context> context();
60
+ v8::Local<v8::Context> new_context(
61
+ v8::Local<v8::ObjectTemplate> global_tmpl = v8::Local<v8::ObjectTemplate>(),
62
+ v8::Local<v8::Value> global_obj = v8::Local<v8::Value>());
76
63
  v8::Local<v8::String> new_string(const char* str);
77
64
  v8::Local<v8::Value> json_parse(v8::Local<v8::Context> context, v8::Local<v8::String> str);
78
65
  v8::Local<v8::String> json_stringify(v8::Local<v8::Context> context, v8::Local<v8::Value> value);
79
-
80
- bool debugger_init(debugger_cb cb, void *cbopq);
81
- bool debugger_send(const std::string& cmd);
82
- void debugger_process();
83
- void debugger_stop();
66
+ std::string to_std_string(v8::Local<v8::Value> value);
84
67
 
85
68
  private:
86
69
  v8::Isolate* isolate_;
87
70
  v8::Persistent<v8::Context> context_;
88
-
89
- DbgSrv* dbg_server_;
90
- v8::Isolate* dbg_isolate_;
91
- debugger_cb callback_;
92
- void* callback_opq_;
93
-
94
- friend class DbgSrv;
95
71
  };
96
72
 
97
73
  } // namespace v8eval
@@ -2,7 +2,9 @@
2
2
 
3
3
  namespace v8eval {
4
4
 
5
- _GoV8::_GoV8() {}
5
+ _GoV8::_GoV8() {
6
+ enable_heap_report();
7
+ }
6
8
 
7
9
  _GoV8::~_GoV8() {}
8
10
 
@@ -1,12 +1,12 @@
1
1
  Gem::Specification.new 'v8eval', '1.0' do |s|
2
2
  s.name = 'v8eval'
3
- s.version = '0.2.3'
3
+ s.version = '0.3.1'
4
4
  s.licenses = ['MIT']
5
5
  s.description = 'Run JavaScript engine V8 in Ruby'
6
- s.summary = 'v8eval gem is ruby binding to the latest V8 4.7 and supports
6
+ s.summary = 'v8eval gem is ruby binding to the latest V8 and supports
7
7
  Linux and Mac OS X.'
8
- s.authors = ['Prateek Papriwal']
9
- s.email = 'prateek.papriwal@jp.sony.com'
8
+ s.authors = ['Prateek Papriwal', 'Yoshiyuki Mineo']
9
+ s.email = 'Yoshiyuki.Mineo@sony.com'
10
10
  s.homepage = 'https://github.com/sony/v8eval'
11
11
  s.extra_rdoc_files = ['README.md']
12
12
 
@@ -17,9 +17,9 @@ Gem::Specification.new 'v8eval', '1.0' do |s|
17
17
  s.require_paths = ['ruby/lib', 'ruby/ext']
18
18
  s.extensions = Dir['ruby/ext/**/extconf.rb']
19
19
 
20
- s.add_development_dependency 'rake', '~> 10.4', '>= 10.4.2'
20
+ s.add_development_dependency 'rake', '>= 12.3.3'
21
21
  s.add_development_dependency 'rspec', '~> 3.0'
22
- s.add_development_dependency 'yard', '0.8.7.6'
22
+ s.add_development_dependency 'yard', '>= 0.9.11'
23
23
 
24
24
  s.required_ruby_version = '>= 2.0.0'
25
25
  end
metadata CHANGED
@@ -1,35 +1,30 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: v8eval
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Prateek Papriwal
8
+ - Yoshiyuki Mineo
8
9
  autorequire:
9
10
  bindir: bin
10
11
  cert_chain: []
11
- date: 2016-04-27 00:00:00.000000000 Z
12
+ date: 2020-12-29 00:00:00.000000000 Z
12
13
  dependencies:
13
14
  - !ruby/object:Gem::Dependency
14
15
  name: rake
15
16
  requirement: !ruby/object:Gem::Requirement
16
17
  requirements:
17
- - - "~>"
18
- - !ruby/object:Gem::Version
19
- version: '10.4'
20
18
  - - ">="
21
19
  - !ruby/object:Gem::Version
22
- version: 10.4.2
20
+ version: 12.3.3
23
21
  type: :development
24
22
  prerelease: false
25
23
  version_requirements: !ruby/object:Gem::Requirement
26
24
  requirements:
27
- - - "~>"
28
- - !ruby/object:Gem::Version
29
- version: '10.4'
30
25
  - - ">="
31
26
  - !ruby/object:Gem::Version
32
- version: 10.4.2
27
+ version: 12.3.3
33
28
  - !ruby/object:Gem::Dependency
34
29
  name: rspec
35
30
  requirement: !ruby/object:Gem::Requirement
@@ -48,18 +43,18 @@ dependencies:
48
43
  name: yard
49
44
  requirement: !ruby/object:Gem::Requirement
50
45
  requirements:
51
- - - '='
46
+ - - ">="
52
47
  - !ruby/object:Gem::Version
53
- version: 0.8.7.6
48
+ version: 0.9.11
54
49
  type: :development
55
50
  prerelease: false
56
51
  version_requirements: !ruby/object:Gem::Requirement
57
52
  requirements:
58
- - - '='
53
+ - - ">="
59
54
  - !ruby/object:Gem::Version
60
- version: 0.8.7.6
55
+ version: 0.9.11
61
56
  description: Run JavaScript engine V8 in Ruby
62
- email: prateek.papriwal@jp.sony.com
57
+ email: Yoshiyuki.Mineo@sony.com
63
58
  executables: []
64
59
  extensions:
65
60
  - ruby/ext/v8eval/extconf.rb
@@ -74,20 +69,11 @@ files:
74
69
  - ruby/Rakefile
75
70
  - ruby/build.sh
76
71
  - ruby/example/js_add.rb
77
- - ruby/ext/v8eval/Makefile
78
72
  - ruby/ext/v8eval/extconf.rb
79
- - ruby/ext/v8eval/v8eval.h
80
73
  - ruby/ext/v8eval/v8eval.i
81
- - ruby/ext/v8eval/v8eval_wrap.cxx
82
74
  - ruby/lib/setup/extension_builder.rb
83
75
  - ruby/lib/v8eval.rb
84
- - ruby/lib/v8eval/Makefile
85
- - ruby/lib/v8eval/extconf.rb
86
- - ruby/lib/v8eval/v8eval.h
87
- - ruby/lib/v8eval/v8eval_wrap.cxx
88
76
  - ruby/spec/v8eval_spec.rb
89
- - src/dbgsrv.cxx
90
- - src/dbgsrv.h
91
77
  - src/v8eval.cxx
92
78
  - src/v8eval.h
93
79
  - src/v8eval_go.cxx
@@ -117,11 +103,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
117
103
  - !ruby/object:Gem::Version
118
104
  version: '0'
119
105
  requirements: []
120
- rubyforge_project:
121
- rubygems_version: 2.5.1
106
+ rubygems_version: 3.1.4
122
107
  signing_key:
123
108
  specification_version: 4
124
- summary: v8eval gem is ruby binding to the latest V8 4.7 and supports Linux and Mac
125
- OS X.
109
+ summary: v8eval gem is ruby binding to the latest V8 and supports Linux and Mac OS
110
+ X.
126
111
  test_files: []
127
- has_rdoc: