Skip to content

Commit 4ed2b26

Browse files
committed
Feature(runtime): Introduce Machine to store whole runtime env (optimize RandomNumberGenerator)
1 parent c3db5f0 commit 4ed2b26

6 files changed

Lines changed: 50 additions & 6 deletions

File tree

packages/runtime/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ set(SOURCE_FILES
1313
jsvalue.h
1414
math.cpp
1515
math.h
16+
machine.cpp
17+
machine.h
1618
v8/src/base/platform/mutex.cc
1719
v8/src/base/platform/time.cc
1820
v8/src/base/platform/semaphore.cc

packages/runtime/machine.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
//
2+
// Created by Dmitry Patsura on 2018-11-28.
3+
//
4+
5+
#include "machine.h"

packages/runtime/machine.h

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//
2+
// Created by Dmitry Patsura on 2018-11-28.
3+
//
4+
5+
#ifndef HLVM_RUNTIME_MACHINE_H
6+
#define HLVM_RUNTIME_MACHINE_H
7+
8+
#include "v8/src/base/utils/random-number-generator.h"
9+
10+
/**
11+
* Store whole Runtime
12+
*/
13+
class Machine {
14+
protected:
15+
v8::base::RandomNumberGenerator* randomNumberGenerator = nullptr;
16+
public:
17+
static Machine& Instance() {
18+
static Machine s;
19+
20+
return s;
21+
}
22+
23+
v8::base::RandomNumberGenerator* getRandomNumberGenerator() {
24+
if (this->randomNumberGenerator) {
25+
return this->randomNumberGenerator;
26+
}
27+
28+
return this->randomNumberGenerator = new v8::base::RandomNumberGenerator();
29+
}
30+
private:
31+
Machine() { }
32+
~Machine() { }
33+
34+
Machine(Machine const&) = delete;
35+
Machine& operator= (Machine const&) = delete;
36+
};
37+
38+
39+
#endif //HLVM_RUNTIME_MACHINE_H

packages/runtime/math.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include <cmath>
22

33
#include "math.h"
4-
#include "v8/src/base/utils/random-number-generator.h"
4+
#include "machine.h"
55

66
LIBRARY_EXPORT double Math__pow(double number, double power) {
77
return std::pow(number, power);
@@ -20,7 +20,5 @@ LIBRARY_EXPORT double Math__round(double number) {
2020
}
2121

2222
LIBRARY_EXPORT double Math__random() {
23-
auto *rng = new v8::base::RandomNumberGenerator();
24-
25-
return rng->NextDouble();
23+
return Machine::Instance().getRandomNumberGenerator()->NextDouble();
2624
}

packages/runtime/package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/runtime/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@static-script/runtime",
3-
"version": "0.8.0",
3+
"version": "0.9.0",
44
"description": "",
55
"main": "index.js",
66
"scripts": {

0 commit comments

Comments
 (0)