Skip to content

Commit 997df41

Browse files
author
Toon Schoenmakers
committed
Actually compile again with latest version of v8
This doesn't run as expected just yet, it only compiles for now. An important thing to note here is that you'll need to copy over 2 files from the v8 build directory these being the natives_blob.bin and the snapshot_blob.bin. It requires these to actually startup v8, but we're embedding it inside PHP-JS.
1 parent dcfeb8a commit 997df41

File tree

6 files changed

+134
-6
lines changed

6 files changed

+134
-6
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
*.o
22
*.so
33
*.d
4+
*_blob.bin
5+
*_blob.h

Makefile

+14-3
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,8 @@ LINKER_DEPENDENCIES = -lphpcpp -lv8
9999

100100
RM = rm -f
101101
CP = cp -f
102-
MKDIR = mkdir -p
102+
MKDIR = mkdir -p
103+
XXD = xxd -i
103104

104105

105106
#
@@ -112,7 +113,7 @@ MKDIR = mkdir -p
112113

113114
SOURCES = $(wildcard *.cpp)
114115
OBJECTS = $(SOURCES:%.cpp=%.o)
115-
DEPENDENCIES = $(SOURCES:%.cpp=%.d)
116+
DEPENDENCIES = $(SOURCES:%.cpp=%.d)
116117

117118

118119
#
@@ -126,10 +127,20 @@ all: ${OBJECTS} ${EXTENSION}
126127
#
127128
-include ${DEPENDENCIES}
128129

130+
natives_blob.h: natives_blob.bin
131+
${CP} natives_blob.bin /tmp/natives_blob.bin
132+
${XXD} /tmp/natives_blob.bin > natives_blob.h
133+
${RM} /tmp/natives_blob.bin
134+
135+
snapshot_blob.h: snapshot_blob.bin
136+
${CP} snapshot_blob.bin /tmp/snapshot_blob.bin
137+
${XXD} /tmp/snapshot_blob.bin > snapshot_blob.h
138+
${RM} /tmp/snapshot_blob.bin
139+
129140
${EXTENSION}: ${OBJECTS}
130141
${LINKER} ${LINKER_FLAGS} -o $@ ${OBJECTS} ${LINKER_DEPENDENCIES}
131142

132-
${OBJECTS}:
143+
${OBJECTS}: snapshot_blob.h natives_blob.h
133144
${COMPILER} ${COMPILER_FLAGS} -o $@ ${@:%.o=%.cpp}
134145

135146
install:

isolate.cpp

+26-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "isolate.h"
2222
#include <cstring>
2323
#include <cstdlib>
24+
#include <iostream>
2425

2526
/**
2627
* Start namespace
@@ -33,6 +34,24 @@ namespace JS {
3334
*/
3435
static thread_local std::unique_ptr<Isolate> isolate;
3536

37+
class ArrayBufferAllocator : public v8::ArrayBuffer::Allocator {
38+
public:
39+
virtual void* Allocate(size_t length) {
40+
void* data = AllocateUninitialized(length);
41+
return data == NULL ? data : memset(data, 0, length);
42+
}
43+
virtual void* AllocateUninitialized(size_t length)
44+
{
45+
return malloc(length);
46+
}
47+
virtual void Free(void* data, size_t)
48+
{
49+
free(data);
50+
}
51+
};
52+
53+
static ArrayBufferAllocator allocator;
54+
3655
/**
3756
* Constructor
3857
*/
@@ -41,8 +60,14 @@ Isolate::Isolate()
4160
// create a platform
4261
Platform::create();
4362

63+
// create our parameters
64+
v8::Isolate::CreateParams params;
65+
66+
// set our custom allocator
67+
params.array_buffer_allocator = &allocator;
68+
4469
// create the actual isolate
45-
_isolate = v8::Isolate::New();
70+
_isolate = v8::Isolate::New(params);
4671

4772
// and enter it
4873
_isolate->Enter();

jsobject.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ namespace JS {
2626
*/
2727
JSObject::Iterator::Iterator(Php::Base *base, const Stack<v8::Object> &object) :
2828
Php::Iterator(base),
29+
_object(object),
2930
_position(0)
3031
{
3132
// create a handle scope, so variables "fall out of scope" and "enter" the context
@@ -35,7 +36,6 @@ JSObject::Iterator::Iterator(Php::Base *base, const Stack<v8::Object> &object) :
3536
// assign variables, this would normally be done inside
3637
// the initializer list, but that way we can't create a
3738
// HandleScope first and v8 refuses to work without one
38-
_object = object;
3939
_keys = _object->GetPropertyNames();
4040
_size = _keys->Length();
4141
}
@@ -172,7 +172,7 @@ Php::Value JSObject::__call(const char *name, Php::Parameters &params)
172172
// create a handle scope, so variables "fall out of scope", "enter" the context and retrieve the value
173173
v8::HandleScope scope(Isolate::get());
174174
v8::Context::Scope context(_object->CreationContext());
175-
v8::Local<v8::Function> function(_object->Get(value(name)).As<v8::Function>());
175+
v8::Local<v8::Function> function(_object->Get(value(Php::Value(name))).As<v8::Function>());
176176
std::vector<v8::Local<v8::Value>> input;
177177

178178
// check whether the value actually exists

platform.cpp

+82
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,60 @@ static std::mutex mutex;
3434
*/
3535
static std::atomic<Platform*> platform;
3636

37+
/**
38+
* Execute a v8::Task with a bit of delay, like we want in the CallDelayedOnForegroundThread
39+
* method.
40+
*/
41+
class DelayedTask : public v8::Task
42+
{
43+
private:
44+
/**
45+
* The underlying task we want to execute
46+
* @var v8::Task
47+
*/
48+
v8::Task *_task;
49+
50+
/**
51+
* The amount of delay that we want before executing the task
52+
* @var double
53+
*/
54+
double _delay;
55+
56+
public:
57+
/**
58+
* Constructor
59+
* @param task
60+
* @param delay
61+
*/
62+
DelayedTask(v8::Task *task, double delay) : _task(task), _delay(delay) {}
63+
64+
/**
65+
* Destructor, we delete the underlying task from here
66+
*/
67+
virtual ~DelayedTask()
68+
{
69+
delete _task;
70+
}
71+
72+
/**
73+
* The abstract Run method of the v8::Task interface
74+
*/
75+
void Run() override
76+
{
77+
// so first we sleep for '_delay' seconds
78+
std::this_thread::sleep_for(std::chrono::duration<double, std::deci>(_delay));
79+
80+
// and then we run the actual task
81+
_task->Run();
82+
}
83+
};
84+
85+
/**
86+
* Include the dumps of the natives and snapshot blobs
87+
*/
88+
#include "natives_blob.h"
89+
#include "snapshot_blob.h"
90+
3791
/**
3892
* Constructor
3993
*/
@@ -87,6 +141,20 @@ void Platform::create()
87141

88142
// initialize the ICU and v8 engine
89143
v8::V8::InitializeICU();
144+
145+
// create a setup a StartupData object for the natives blob
146+
v8::StartupData natives;
147+
natives.data = (const char*) _tmp_natives_blob_bin;
148+
natives.raw_size = _tmp_natives_blob_bin_len;
149+
v8::V8::SetNativesDataBlob(&natives);
150+
151+
// create a setup a StartupData object for the snapshot blob
152+
v8::StartupData snapshot;
153+
snapshot.data = (const char*) _tmp_snapshot_blob_bin;
154+
snapshot.raw_size = _tmp_snapshot_blob_bin_len;
155+
v8::V8::SetSnapshotDataBlob(&snapshot);
156+
157+
// initialize the platform
90158
v8::V8::InitializePlatform(result);
91159
v8::V8::Initialize();
92160

@@ -238,6 +306,20 @@ void Platform::CallOnForegroundThread(v8::Isolate *isolate, v8::Task *task)
238306
*/
239307
}
240308

309+
/**
310+
* Schedules a task to be invoked on a foreground thread wrt a specific
311+
* |isolate| after the given number of seconds |delay_in_seconds|.
312+
* Tasks posted for the same isolate should be execute in order of
313+
* scheduling. The definition of "foreground" is opaque to V8.
314+
*/
315+
void Platform::CallDelayedOnForegroundThread(v8::Isolate *isolate, v8::Task *task, double delay_in_seconds)
316+
{
317+
// we simply call the CallOnBackgroundThread method which will queue our task, but before that
318+
// we turn it into a DelayedTask. The ExpectedRuntime here doesn't matter as our implementation
319+
// of CallOnBackgroundThread doesn't do anything with it anyway
320+
CallOnBackgroundThread(new DelayedTask(task, delay_in_seconds), ExpectedRuntime::kShortRunningTask);
321+
}
322+
241323
/**
242324
* Retrieve the monotonically increasing time. The starting point
243325
* is not relevant, but it must return at least millisecond-precision

platform.h

+8
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,14 @@ class Platform : public v8::Platform
114114
*/
115115
void CallOnForegroundThread(v8::Isolate *isolate, v8::Task *task) override;
116116

117+
/**
118+
* Schedules a task to be invoked on a foreground thread wrt a specific
119+
* |isolate| after the given number of seconds |delay_in_seconds|.
120+
* Tasks posted for the same isolate should be execute in order of
121+
* scheduling. The definition of "foreground" is opaque to V8.
122+
*/
123+
void CallDelayedOnForegroundThread(v8::Isolate *isolate, v8::Task *task, double delay_in_seconds) override;
124+
117125
/**
118126
* Retrieve the monotonically increasing time. The starting point
119127
* is not relevant, but it must return at least millisecond-precision

0 commit comments

Comments
 (0)