Skip to content

Commit

Permalink
Actually compile again with latest version of v8
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Toon Schoenmakers committed Apr 11, 2016
1 parent dcfeb8a commit 997df41
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 6 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
*.o
*.so
*.d
*_blob.bin
*_blob.h
17 changes: 14 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ LINKER_DEPENDENCIES = -lphpcpp -lv8

RM = rm -f
CP = cp -f
MKDIR = mkdir -p
MKDIR = mkdir -p
XXD = xxd -i


#
Expand All @@ -112,7 +113,7 @@ MKDIR = mkdir -p

SOURCES = $(wildcard *.cpp)
OBJECTS = $(SOURCES:%.cpp=%.o)
DEPENDENCIES = $(SOURCES:%.cpp=%.d)
DEPENDENCIES = $(SOURCES:%.cpp=%.d)


#
Expand All @@ -126,10 +127,20 @@ all: ${OBJECTS} ${EXTENSION}
#
-include ${DEPENDENCIES}

natives_blob.h: natives_blob.bin
${CP} natives_blob.bin /tmp/natives_blob.bin
${XXD} /tmp/natives_blob.bin > natives_blob.h
${RM} /tmp/natives_blob.bin

snapshot_blob.h: snapshot_blob.bin
${CP} snapshot_blob.bin /tmp/snapshot_blob.bin
${XXD} /tmp/snapshot_blob.bin > snapshot_blob.h
${RM} /tmp/snapshot_blob.bin

${EXTENSION}: ${OBJECTS}
${LINKER} ${LINKER_FLAGS} -o $@ ${OBJECTS} ${LINKER_DEPENDENCIES}

${OBJECTS}:
${OBJECTS}: snapshot_blob.h natives_blob.h
${COMPILER} ${COMPILER_FLAGS} -o $@ ${@:%.o=%.cpp}

install:
Expand Down
27 changes: 26 additions & 1 deletion isolate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "isolate.h"
#include <cstring>
#include <cstdlib>
#include <iostream>

/**
* Start namespace
Expand All @@ -33,6 +34,24 @@ namespace JS {
*/
static thread_local std::unique_ptr<Isolate> isolate;

class ArrayBufferAllocator : public v8::ArrayBuffer::Allocator {
public:
virtual void* Allocate(size_t length) {
void* data = AllocateUninitialized(length);
return data == NULL ? data : memset(data, 0, length);
}
virtual void* AllocateUninitialized(size_t length)
{
return malloc(length);
}
virtual void Free(void* data, size_t)
{
free(data);
}
};

static ArrayBufferAllocator allocator;

/**
* Constructor
*/
Expand All @@ -41,8 +60,14 @@ Isolate::Isolate()
// create a platform
Platform::create();

// create our parameters
v8::Isolate::CreateParams params;

// set our custom allocator
params.array_buffer_allocator = &allocator;

// create the actual isolate
_isolate = v8::Isolate::New();
_isolate = v8::Isolate::New(params);

// and enter it
_isolate->Enter();
Expand Down
4 changes: 2 additions & 2 deletions jsobject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ namespace JS {
*/
JSObject::Iterator::Iterator(Php::Base *base, const Stack<v8::Object> &object) :
Php::Iterator(base),
_object(object),
_position(0)
{
// create a handle scope, so variables "fall out of scope" and "enter" the context
Expand All @@ -35,7 +36,6 @@ JSObject::Iterator::Iterator(Php::Base *base, const Stack<v8::Object> &object) :
// assign variables, this would normally be done inside
// the initializer list, but that way we can't create a
// HandleScope first and v8 refuses to work without one
_object = object;
_keys = _object->GetPropertyNames();
_size = _keys->Length();
}
Expand Down Expand Up @@ -172,7 +172,7 @@ Php::Value JSObject::__call(const char *name, Php::Parameters &params)
// create a handle scope, so variables "fall out of scope", "enter" the context and retrieve the value
v8::HandleScope scope(Isolate::get());
v8::Context::Scope context(_object->CreationContext());
v8::Local<v8::Function> function(_object->Get(value(name)).As<v8::Function>());
v8::Local<v8::Function> function(_object->Get(value(Php::Value(name))).As<v8::Function>());
std::vector<v8::Local<v8::Value>> input;

// check whether the value actually exists
Expand Down
82 changes: 82 additions & 0 deletions platform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,60 @@ static std::mutex mutex;
*/
static std::atomic<Platform*> platform;

/**
* Execute a v8::Task with a bit of delay, like we want in the CallDelayedOnForegroundThread
* method.
*/
class DelayedTask : public v8::Task
{
private:
/**
* The underlying task we want to execute
* @var v8::Task
*/
v8::Task *_task;

/**
* The amount of delay that we want before executing the task
* @var double
*/
double _delay;

public:
/**
* Constructor
* @param task
* @param delay
*/
DelayedTask(v8::Task *task, double delay) : _task(task), _delay(delay) {}

/**
* Destructor, we delete the underlying task from here
*/
virtual ~DelayedTask()
{
delete _task;
}

/**
* The abstract Run method of the v8::Task interface
*/
void Run() override
{
// so first we sleep for '_delay' seconds
std::this_thread::sleep_for(std::chrono::duration<double, std::deci>(_delay));

// and then we run the actual task
_task->Run();
}
};

/**
* Include the dumps of the natives and snapshot blobs
*/
#include "natives_blob.h"
#include "snapshot_blob.h"

/**
* Constructor
*/
Expand Down Expand Up @@ -87,6 +141,20 @@ void Platform::create()

// initialize the ICU and v8 engine
v8::V8::InitializeICU();

// create a setup a StartupData object for the natives blob
v8::StartupData natives;
natives.data = (const char*) _tmp_natives_blob_bin;
natives.raw_size = _tmp_natives_blob_bin_len;
v8::V8::SetNativesDataBlob(&natives);

// create a setup a StartupData object for the snapshot blob
v8::StartupData snapshot;
snapshot.data = (const char*) _tmp_snapshot_blob_bin;
snapshot.raw_size = _tmp_snapshot_blob_bin_len;
v8::V8::SetSnapshotDataBlob(&snapshot);

// initialize the platform
v8::V8::InitializePlatform(result);
v8::V8::Initialize();

Expand Down Expand Up @@ -238,6 +306,20 @@ void Platform::CallOnForegroundThread(v8::Isolate *isolate, v8::Task *task)
*/
}

/**
* Schedules a task to be invoked on a foreground thread wrt a specific
* |isolate| after the given number of seconds |delay_in_seconds|.
* Tasks posted for the same isolate should be execute in order of
* scheduling. The definition of "foreground" is opaque to V8.
*/
void Platform::CallDelayedOnForegroundThread(v8::Isolate *isolate, v8::Task *task, double delay_in_seconds)
{
// we simply call the CallOnBackgroundThread method which will queue our task, but before that
// we turn it into a DelayedTask. The ExpectedRuntime here doesn't matter as our implementation
// of CallOnBackgroundThread doesn't do anything with it anyway
CallOnBackgroundThread(new DelayedTask(task, delay_in_seconds), ExpectedRuntime::kShortRunningTask);
}

/**
* Retrieve the monotonically increasing time. The starting point
* is not relevant, but it must return at least millisecond-precision
Expand Down
8 changes: 8 additions & 0 deletions platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,14 @@ class Platform : public v8::Platform
*/
void CallOnForegroundThread(v8::Isolate *isolate, v8::Task *task) override;

/**
* Schedules a task to be invoked on a foreground thread wrt a specific
* |isolate| after the given number of seconds |delay_in_seconds|.
* Tasks posted for the same isolate should be execute in order of
* scheduling. The definition of "foreground" is opaque to V8.
*/
void CallDelayedOnForegroundThread(v8::Isolate *isolate, v8::Task *task, double delay_in_seconds) override;

/**
* Retrieve the monotonically increasing time. The starting point
* is not relevant, but it must return at least millisecond-precision
Expand Down

0 comments on commit 997df41

Please sign in to comment.