Skip to content

Commit

Permalink
refactor: update pandio
Browse files Browse the repository at this point in the history
  • Loading branch information
michaldziuba03 committed Sep 26, 2024
1 parent b9e59e2 commit 1ed1164
Show file tree
Hide file tree
Showing 13 changed files with 94 additions and 115 deletions.
6 changes: 3 additions & 3 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[submodule "luxio"]
path = deps/luxio
url = https://github.com/michaldziuba03/luxio
[submodule "pandio"]
path = deps/pandio
url = https://github.com/pandland
14 changes: 6 additions & 8 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,25 @@ add_definitions(-DV8_ENABLE_SANDBOX=1 -DV8_COMPRESS_POINTERS=1)

add_executable(pand core/main.cc)

target_include_directories(pand SYSTEM
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/deps/v8/include)

target_include_directories(pand
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/deps/luxio/src
${CMAKE_CURRENT_SOURCE_DIR}/deps/v8/include
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/deps/swcc/include)

# Add pandio library
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/deps/pandio pandio_build)

target_link_directories(pand
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/deps/v8/out.gn/x64.release.sample/obj
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/deps/luxio/build
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/deps/swcc/target/release)


target_link_libraries(pand
swcc
luxio
pandio
v8_libplatform
v8_monolith
pthread
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@ You need to download v8 as dependency and it will take reasonable amount of time
```sh
# Download v8 and build
cmake -P v8.cmake

# Build our actual project
mkdir build
cd build

# Build our actual project
cmake ..
cmake --build .
```
Expand Down
14 changes: 7 additions & 7 deletions core/io.cc
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
#pragma once
#include <luxio.h>
#include <pandio.h>

namespace runtime {

/* IO singleton - manages actuall event loop */
/* IO singleton - manages actual event loop */
class IO {
static IO *instance_;
public:
lx_io_t *ctx;
pd_io_t *ctx;

IO(lx_io_t *ctx): ctx(ctx) {}
IO(pd_io_t *ctx): ctx(ctx) {}
~IO() {
delete ctx;
}

static void create() {
lx_io_t *ctx = new lx_io_t;
lx_init(ctx);
pd_io_t *ctx = new pd_io_t;
pd_io_init(ctx);
IO:instance_ = new IO(ctx);
}

Expand All @@ -25,7 +25,7 @@ class IO {
}

inline void run() {
lx_run(this->ctx);
pd_io_run(this->ctx);
}
};

Expand Down
1 change: 0 additions & 1 deletion core/js_internals.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

#include <unordered_map>
#include <string>
#include <vector>

std::unordered_map<std::string, std::vector<unsigned char>> js_internals = {
{"std:assert", { 0x65, 0x78, 0x70, 0x6f, 0x72, 0x74, 0x20, 0x63, 0x6c, 0x61, 0x73, 0x73, 0x20, 0x41, 0x73, 0x73, 0x65, 0x72, 0x74, 0x69, 0x6f, 0x6e, 0x45, 0x72, 0x72, 0x6f, 0x72, 0x20, 0x65, 0x78, 0x74, 0x65, 0x6e, 0x64, 0x73, 0x20, 0x45, 0x72, 0x72, 0x6f, 0x72, 0x20, 0x7b, 0x0a, 0x20, 0x20, 0x63, 0x6f, 0x6e, 0x73, 0x74, 0x72, 0x75, 0x63, 0x74, 0x6f, 0x72, 0x28, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x29, 0x20, 0x7b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x73, 0x75, 0x70, 0x65, 0x72, 0x28, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x29, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x74, 0x68, 0x69, 0x73, 0x2e, 0x6e, 0x61, 0x6d, 0x65, 0x20, 0x3d, 0x20, 0x27, 0x41, 0x73, 0x73, 0x65, 0x72, 0x74, 0x69, 0x6f, 0x6e, 0x45, 0x72, 0x72, 0x6f, 0x72, 0x27, 0x0a, 0x20, 0x20, 0x7d, 0x0a, 0x7d, 0x0a, 0x0a, 0x65, 0x78, 0x70, 0x6f, 0x72, 0x74, 0x20, 0x66, 0x75, 0x6e, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x20, 0x61, 0x73, 0x73, 0x65, 0x72, 0x74, 0x28, 0x63, 0x6f, 0x6e, 0x64, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x2c, 0x20, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x29, 0x20, 0x7b, 0x0a, 0x20, 0x20, 0x69, 0x66, 0x20, 0x28, 0x21, 0x63, 0x6f, 0x6e, 0x64, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x29, 0x20, 0x7b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x74, 0x68, 0x72, 0x6f, 0x77, 0x20, 0x6e, 0x65, 0x77, 0x20, 0x41, 0x73, 0x73, 0x65, 0x72, 0x74, 0x69, 0x6f, 0x6e, 0x45, 0x72, 0x72, 0x6f, 0x72, 0x28, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x20, 0x7c, 0x7c, 0x20, 0x22, 0x41, 0x73, 0x73, 0x65, 0x72, 0x74, 0x69, 0x6f, 0x6e, 0x20, 0x66, 0x61, 0x69, 0x6c, 0x65, 0x64, 0x22, 0x29, 0x3b, 0x0a, 0x20, 0x20, 0x7d, 0x0a, 0x7d, 0x0a, 0x0a, 0x65, 0x78, 0x70, 0x6f, 0x72, 0x74, 0x20, 0x66, 0x75, 0x6e, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x20, 0x61, 0x73, 0x73, 0x65, 0x72, 0x74, 0x53, 0x74, 0x72, 0x69, 0x63, 0x74, 0x45, 0x71, 0x75, 0x61, 0x6c, 0x28, 0x61, 0x63, 0x74, 0x75, 0x61, 0x6c, 0x2c, 0x20, 0x65, 0x78, 0x70, 0x65, 0x63, 0x74, 0x65, 0x64, 0x2c, 0x20, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x29, 0x20, 0x7b, 0x0a, 0x20, 0x20, 0x69, 0x66, 0x20, 0x28, 0x21, 0x4f, 0x62, 0x6a, 0x65, 0x63, 0x74, 0x2e, 0x69, 0x73, 0x28, 0x61, 0x63, 0x74, 0x75, 0x61, 0x6c, 0x2c, 0x20, 0x65, 0x78, 0x70, 0x65, 0x63, 0x74, 0x65, 0x64, 0x29, 0x29, 0x20, 0x7b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x74, 0x68, 0x72, 0x6f, 0x77, 0x20, 0x6e, 0x65, 0x77, 0x20, 0x41, 0x73, 0x73, 0x65, 0x72, 0x74, 0x69, 0x6f, 0x6e, 0x45, 0x72, 0x72, 0x6f, 0x72, 0x28, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x20, 0x7c, 0x7c, 0x20, 0x22, 0x41, 0x73, 0x73, 0x65, 0x72, 0x74, 0x20, 0x73, 0x74, 0x72, 0x69, 0x63, 0x74, 0x20, 0x65, 0x71, 0x75, 0x61, 0x6c, 0x20, 0x66, 0x61, 0x69, 0x6c, 0x65, 0x64, 0x22, 0x29, 0x3b, 0x0a, 0x20, 0x20, 0x7d, 0x0a, 0x7d, 0x0a, 0x0a, 0x65, 0x78, 0x70, 0x6f, 0x72, 0x74, 0x20, 0x66, 0x75, 0x6e, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x20, 0x61, 0x73, 0x73, 0x65, 0x72, 0x74, 0x54, 0x68, 0x72, 0x6f, 0x77, 0x73, 0x28, 0x66, 0x75, 0x6e, 0x63, 0x2c, 0x20, 0x65, 0x72, 0x72, 0x43, 0x6c, 0x61, 0x73, 0x73, 0x20, 0x3d, 0x20, 0x45, 0x72, 0x72, 0x6f, 0x72, 0x2c, 0x20, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x29, 0x20, 0x7b, 0x0a, 0x20, 0x20, 0x74, 0x72, 0x79, 0x20, 0x7b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x66, 0x75, 0x6e, 0x63, 0x28, 0x29, 0x3b, 0x0a, 0x20, 0x20, 0x7d, 0x20, 0x63, 0x61, 0x74, 0x63, 0x68, 0x20, 0x28, 0x65, 0x72, 0x72, 0x29, 0x20, 0x7b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x69, 0x66, 0x20, 0x28, 0x65, 0x72, 0x72, 0x20, 0x69, 0x6e, 0x73, 0x74, 0x61, 0x6e, 0x63, 0x65, 0x6f, 0x66, 0x20, 0x65, 0x72, 0x72, 0x43, 0x6c, 0x61, 0x73, 0x73, 0x29, 0x20, 0x7b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x72, 0x65, 0x74, 0x75, 0x72, 0x6e, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x7d, 0x0a, 0x20, 0x20, 0x7d, 0x0a, 0x0a, 0x20, 0x20, 0x74, 0x68, 0x72, 0x6f, 0x77, 0x20, 0x6e, 0x65, 0x77, 0x20, 0x41, 0x73, 0x73, 0x65, 0x72, 0x74, 0x69, 0x6f, 0x6e, 0x45, 0x72, 0x72, 0x6f, 0x72, 0x28, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x20, 0x7c, 0x7c, 0x20, 0x22, 0x46, 0x75, 0x6e, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x20, 0x64, 0x6f, 0x65, 0x73, 0x20, 0x6e, 0x6f, 0x74, 0x20, 0x74, 0x68, 0x72, 0x6f, 0x77, 0x20, 0x45, 0x72, 0x72, 0x6f, 0x72, 0x22, 0x29, 0x3b, 0x0a, 0x7d, 0x0a }},
Expand Down
2 changes: 0 additions & 2 deletions core/main.cc
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#include <v8.h>
#include <luxio.h>
#include <signal.h>

#include "runtime.cc"

int main(int argc, char* argv[]) {
Expand Down
34 changes: 9 additions & 25 deletions core/runtime.cc
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#include <v8.h>
#include <libplatform/libplatform.h>
#include <filesystem>
#include <luxio.h>
#include <pandio.h>

#include "loader.cc"
#include "pandio/core.h"
#include "timers.cc"
#include "io.cc"
#include "tcp.cc"
Expand Down Expand Up @@ -64,12 +65,13 @@ class Runtime {
}

void start(const char *entryfile, int argc, char* argv[]) {
IO::create();
pd_io_t *ctx = IO::get()->ctx;

v8::HandleScope handle_scope(isolate);
v8::Local<v8::ObjectTemplate> global = v8::ObjectTemplate::New(isolate);

std::string entrypath = fs::absolute(entryfile).string();
IO::create();
lx_io_t *ctx = IO::get()->ctx;

v8::Local<v8::ObjectTemplate> runtime_template = v8::ObjectTemplate::New(isolate);
Runtime::initialize(runtime_template, isolate);
Expand All @@ -91,8 +93,8 @@ class Runtime {
loader.execute(isolate, context, "std:bootstrap");
loader.execute(isolate, context, entrypath);

std::thread memory_monitor(MonitorMemoryUsage, isolate);
memory_monitor.detach();
//std::thread memory_monitor(MonitorMemoryUsage, isolate);
//memory_monitor.detach();

IO::get()->run();
}
Expand Down Expand Up @@ -132,11 +134,7 @@ class Runtime {
}

static int get_pid() {
#if defined(_WIN32) || defined(_WIN64)
return GetCurrentProcessId();
#else
return getpid();
#endif
return pd_getpid();
}

static void cwd(const v8::FunctionCallbackInfo<v8::Value> &args) {
Expand All @@ -152,21 +150,7 @@ class Runtime {
}

static const char *get_platform() {
#if defined(_WIN32) || defined(_WIN64)
return "win32";
#elif defined(__APPLE__) || defined(__MACH__)
return "darwin";
#elif defined(__linux__)
return "linux";
#elif defined(__FreeBSD__)
return "freebsd";
#elif defined(__OpenBSD__)
return "openbsd";
#elif defined(__unix__)
return "unix";
#else
return "unknown";
#endif
return pd_get_platform();
}

static void env(const v8::FunctionCallbackInfo<v8::Value> &args) {
Expand Down
89 changes: 45 additions & 44 deletions core/tcp.cc
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#pragma once

#include <cstdio>
#include <v8.h>
#include <luxio.h>
#include <pandio.h>
#include <assert.h>

#include <string.h>
Expand All @@ -12,14 +13,14 @@ namespace runtime {

class TcpStream {
public:
lx_connection_t *conn;
lx_timer_t timeout;
pd_tcp_t conn;
pd_timer_t timeout;
bool paused = false;
v8::Persistent<v8::Object> obj;

TcpStream(v8::Isolate *isolate, v8::Local<v8::Object> obj) {
this->obj.Reset(isolate, obj);
lx_timer_init(IO::get()->ctx, &this->timeout);
pd_timer_init(IO::get()->ctx, &this->timeout);
this->timeout.data = this;
}

Expand Down Expand Up @@ -73,16 +74,16 @@ class TcpStream {
TcpStream *stream = static_cast<TcpStream*>(args.This()->GetAlignedPointerFromInternalField(0));
char *buf = strdup(*str);

lx_write_t *write_op = lx_write_alloc(buf, str.length());
write_op->data = stream;
lx_write(write_op, stream->conn, TcpStream::handle_write);
pd_write_t *write_op = new pd_write_t;
pd_write_init(write_op, buf, str.length(), TcpStream::handle_write);
pd_tcp_write(&stream->conn, write_op);
}

static void close(const v8::FunctionCallbackInfo<v8::Value> &args) {
assert(args.Length() == 0);
TcpStream *stream = static_cast<TcpStream*>(args.This()->GetAlignedPointerFromInternalField(0));

lx_close(stream->conn);
pd_tcp_close(&stream->conn);
}

static void set_timeout(const v8::FunctionCallbackInfo<v8::Value> &args) {
Expand All @@ -98,12 +99,12 @@ class TcpStream {
TcpStream *stream = static_cast<TcpStream*>(args.This()->GetAlignedPointerFromInternalField(0));

if (timeout == 0) {
lx_timer_stop(&stream->timeout);
pd_timer_stop(&stream->timeout);
return;
}

lx_timer_stop(&stream->timeout);
lx_timer_start(&stream->timeout, TcpStream::handle_timeout, timeout);
pd_timer_stop(&stream->timeout);
pd_timer_start(&stream->timeout, TcpStream::handle_timeout, timeout);
}

static void pause(const v8::FunctionCallbackInfo<v8::Value> &args) {
Expand All @@ -112,7 +113,7 @@ class TcpStream {

TcpStream *stream = static_cast<TcpStream*>(args.This()->GetAlignedPointerFromInternalField(0));
if (!stream->paused) {
lx_stop_reading(&stream->conn->event, stream->conn->fd);
pd_tcp_pause(&stream->conn);
stream->paused = true;
}
}
Expand All @@ -123,18 +124,19 @@ class TcpStream {

TcpStream *stream = static_cast<TcpStream*>(args.This()->GetAlignedPointerFromInternalField(0));
if (stream->paused) {
lx_set_read_event(&stream->conn->event, stream->conn->fd);
pd_tcp_resume(&stream->conn);
stream->paused = false;
}
}

/* handlers for event loop */
static void handle_write(lx_write_t *write_op, int status) {
free((void*)write_op->buf);
free(write_op);
static void handle_write(pd_write_t *write_op, int status) {
free((void*)write_op->data.buf);
delete write_op;
}

static void handle_data(lx_connection_t *conn) {
static void handle_data(pd_tcp_t *conn, char *buf, size_t size) {
printf("Status: %ld\n", size); fflush(stdout);
TcpStream *stream = static_cast<TcpStream*>(conn->data);

v8::Isolate *isolate = v8::Isolate::GetCurrent();
Expand All @@ -147,40 +149,34 @@ class TcpStream {
return;
}

char buf[1024];
ssize_t res = recv(conn->fd, buf, 1024, 0);
if (res <= 0) {
lx_close(conn);
return;
}

v8::Local<v8::Value> args[1] = {v8::String::NewFromUtf8(isolate, buf, v8::NewStringType::kNormal, res).ToLocalChecked()};
v8::Local<v8::Value> args[1] = {v8::String::NewFromUtf8(isolate, buf, v8::NewStringType::kNormal, size).ToLocalChecked()};
callback->Call(context, v8::Undefined(isolate), 1, args).ToLocalChecked();
}

static void handle_close(lx_connection_t *conn) {
static void handle_close(pd_tcp_t *conn) {
v8::Isolate *isolate = v8::Isolate::GetCurrent();
v8::HandleScope handle_scope(isolate);
v8::Local<v8::Context> context = isolate->GetCurrentContext();

TcpStream *stream = static_cast<TcpStream*>(conn->data);
lx_timer_stop(&stream->timeout);
pd_timer_stop(&stream->timeout);
v8::Local<v8::Function> callback =
stream->obj.Get(isolate)->Get(context, v8_symbol(isolate, "onclose")).ToLocalChecked().As<v8::Function>();
callback->Call(context, v8::Undefined(isolate), 0, {}).ToLocalChecked();
delete stream;
//delete stream;
}

static void handle_timeout(lx_timer_t *timer) {
static void handle_timeout(pd_timer_t *timer) {
TcpStream *stream = static_cast<TcpStream*>(timer->data);
lx_close(stream->conn);
pd_tcp_close(&stream->conn);
}
};

v8::Persistent<v8::Function> TcpStream::streamConstructor;

class TcpServer {
v8::Global<v8::Function> callback;
pd_tcp_server_t handle;
int port;

static const int64_t kDefaultTimeout = 120 * 1000;
Expand All @@ -204,31 +200,36 @@ class TcpServer {
server->callback.Reset(isolate, callback);
server->port = port;

lx_listener_t *l = lx_listen(IO::get()->ctx, port, TcpServer::handle_accept);
l->data = server;
pd_tcp_server_init(IO::get()->ctx, &server->handle);
server->handle.data = server;
int status = pd_tcp_listen(&server->handle, port, TcpServer::handle_accept);
if (status < 0) {
printf("Something went wrong...");
}
}

static void handle_accept(lx_connection_t *conn) {
TcpServer *server = static_cast<TcpServer*>(conn->listener->data);
static void handle_accept(pd_tcp_server_t *handle, pd_socket_t socket, int status) {
TcpServer *server = static_cast<TcpServer*>(handle->data);
v8::Isolate *isolate = v8::Isolate::GetCurrent();
v8::HandleScope handle_scope(isolate);

v8::Local<v8::Context> context = isolate->GetCurrentContext();
v8::Local<v8::Function> callback = server->callback.Get(isolate);

if (callback.IsEmpty()) {
lx_close(conn);
return;
}

v8::Local<v8::Function> cons = v8::Local<v8::Function>::New(isolate, TcpStream::streamConstructor);
v8::Local<v8::Object> tcpStreamWrap = cons->NewInstance(context).ToLocalChecked();
TcpStream *stream = static_cast<TcpStream*>(tcpStreamWrap->GetAlignedPointerFromInternalField(0));
stream->conn = conn;
lx_timer_start(&stream->timeout, TcpStream::handle_timeout, TcpServer::kDefaultTimeout);
conn->data = (void*)stream;
conn->onclose = TcpStream::handle_close;
conn->ondata = TcpStream::handle_data;
pd_tcp_init(handle->ctx, &stream->conn);
pd_tcp_accept(&stream->conn, socket);
pd_timer_start(&stream->timeout, TcpStream::handle_timeout, TcpServer::kDefaultTimeout);
stream->conn.data = (void*)stream;
stream->conn.on_close = TcpStream::handle_close;
stream->conn.on_data = TcpStream::handle_data;

if (callback.IsEmpty()) {
pd_tcp_close(&stream->conn);
return;
}

v8::Local<v8::Value> args[1] = {tcpStreamWrap};
callback->Call(context, v8::Undefined(isolate), 1, args).ToLocalChecked();
Expand Down
Loading

0 comments on commit 1ed1164

Please sign in to comment.