Skip to content

Commit

Permalink
clang-format-11 => clang-format-15
Browse files Browse the repository at this point in the history
  • Loading branch information
yoursunny committed Jan 20, 2024
1 parent a024d1a commit 622d953
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 70 deletions.
12 changes: 9 additions & 3 deletions .clang-format
Expand Up @@ -3,8 +3,14 @@ BasedOnStyle: Mozilla
AllowShortFunctionsOnASingleLine: Empty
BinPackArguments: true
BinPackParameters: true
BreakBeforeBraces: Attach
ColumnLimit: 100
IncludeIsMainRegex: '[.]t$'
Cpp11BracedListStyle: true
FixNamespaceComments: true
IncludeIsMainRegex: '(\.t)?$'
InsertBraces: true
QualifierAlignment: Custom
QualifierOrder: ['static', 'inline', 'const', 'constexpr', 'volatile', 'type', 'restrict']
ReflowComments: false
SortIncludes: true
SortUsingDeclarations: true
ShortNamespaceLines: 1000000
SpacesInContainerLiterals: false
10 changes: 5 additions & 5 deletions .github/workflows/build.yml
Expand Up @@ -3,16 +3,16 @@ on:
push:
pull_request:
workflow_dispatch:

permissions: {}
jobs:
build:
runs-on: ubuntu-20.04
steps:
- uses: actions/setup-node@v3
- uses: actions/setup-node@v4
with:
node-version: 16
node-version: 18
- name: Download VPP
uses: actions/checkout@v3
uses: actions/checkout@v4
with:
repository: FDio/vpp
ref: v23.06
Expand All @@ -26,7 +26,7 @@ jobs:
make
sudo make install
sudo ldconfig
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- name: Build and test
run: |
corepack pnpm install
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
@@ -1,6 +1,6 @@
ISC License

Copyright (c) 2021-2023, Junxiao Shi
Copyright (c) 2021-2024, Junxiao Shi

Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
Expand Down
7 changes: 4 additions & 3 deletions lib/memif.ts
Expand Up @@ -34,7 +34,7 @@ function newNativeMemif(opts: NativeMemifOptions): NativeMemif {
suggest = `os=${process.platform} cpu=${process.arch} are not supported`;
} else if (!fs.existsSync("/usr/local/lib/libmemif.so")) {
suggest = "/usr/local/lib/libmemif.so does not exist, reinstall libmemif";
} else if (!fs.existsSync(path.resolve(path.dirname(fileURLToPath(import.meta.url)), "../build/Release/memif-native.node"))) {
} else if (!fs.existsSync(fileURLToPath(new URL("../build/Release/memif-native.node", import.meta.url)))) {
suggest = "memif-native.node does not exist, reinstall node-memif";
}
throw new Error(`cannot load memif C++ addon: ${suggest}\n${err}`);
Expand All @@ -49,7 +49,7 @@ const activeSocketNames = new Set<string>();
*
* This class wraps libmemif as a Duplex stream in object mode.
* Received packets can be read from the stream as Uint8Array.
* To transmit a packet, write a ArrayBufferView or ArrayBuffer to the stream.
* To transmit a packet, write an ArrayBufferView or ArrayBuffer to the stream.
*/
export class Memif extends Duplex {
constructor({
Expand Down Expand Up @@ -164,7 +164,8 @@ export class Memif extends Duplex {

if (this.rxChunks.length > 0) {
this.rxChunks.push(b);
b = Buffer.concat(this.rxChunks.splice(0, Infinity));
b = Buffer.concat(this.rxChunks);
this.rxChunks = [];
}
this.push(b);
};
Expand Down
18 changes: 9 additions & 9 deletions package.json
Expand Up @@ -24,24 +24,24 @@
},
"scripts": {
"build": "tsc",
"clang-format": "git ls-files '*.cc' | xargs clang-format-11 -i -style=file",
"clang-format": "git ls-files '*.cc' | xargs clang-format-15 -i -style=file",
"install": "node-gyp rebuild",
"lint": "xo --fix",
"lint": "xo-yoursunny --fix",
"test": "node ./test/main.js",
"typecheck": "tsc -p ./test"
},
"gypfile": true,
"packageManager": "pnpm@8.6.7",
"packageManager": "pnpm@8.14.1",
"dependencies": {
"node-addon-api": "^7.0.0"
"node-addon-api": "^7.1.0"
},
"devDependencies": {
"@types/node": "^20.4.2",
"@types/tmp": "^0.2.3",
"@yoursunny/xo-config": "^0.54.0",
"execa": "^7.1.1",
"@types/node": "^20.11.5",
"@types/tmp": "^0.2.6",
"@yoursunny/xo-config": "^0.56.2",
"execa": "^8.0.1",
"p-event": "^6.0.0",
"tmp": "^0.2.1",
"typescript": "~5.1.6"
"typescript": "~5.3.3"
}
}
72 changes: 25 additions & 47 deletions src/memif.cc
Expand Up @@ -10,16 +10,13 @@

#include <napi.h>
#include <uv.h>
extern "C"
{
extern "C" {
#include <libmemif.h>
}

class Memif : public Napi::ObjectWrap<Memif>
{
class Memif : public Napi::ObjectWrap<Memif> {
public:
static Napi::Object Init(Napi::Env env, Napi::Object exports)
{
static Napi::Object Init(Napi::Env env, Napi::Object exports) {
auto func = DefineClass(env, "Memif",
{
InstanceAccessor("counters", &Memif::counters, nullptr),
Expand All @@ -34,8 +31,7 @@ class Memif : public Napi::ObjectWrap<Memif>
}

explicit Memif(const Napi::CallbackInfo& info)
: Napi::ObjectWrap<Memif>(info)
{
: Napi::ObjectWrap<Memif>(info) {
auto env = info.Env();

auto args = info[0].ToObject();
Expand Down Expand Up @@ -80,21 +76,18 @@ class Memif : public Napi::ObjectWrap<Memif>
m_running = true;
}

static Napi::Value CreateNewItem(const Napi::CallbackInfo& info)
{
static Napi::Value CreateNewItem(const Napi::CallbackInfo& info) {
auto env = info.Env();
auto ctor = env.GetInstanceData<Napi::FunctionReference>();
return ctor->New({ info[0] });
return ctor->New({info[0]});
}

void Finalize(Napi::Env env) override
{
void Finalize(Napi::Env env) override {
stop();
}

private:
Napi::Value counters(const Napi::CallbackInfo& info)
{
Napi::Value counters(const Napi::CallbackInfo& info) {
auto env = info.Env();
auto cnt = Napi::Object::New(env);
cnt.Set("nRxPackets", Napi::BigInt::New(env, m_nRxPackets));
Expand All @@ -105,8 +98,7 @@ class Memif : public Napi::ObjectWrap<Memif>
return cnt;
}

void send(const Napi::CallbackInfo& info)
{
void send(const Napi::CallbackInfo& info) {
auto buffer = info[0].As<Napi::ArrayBuffer>();
uint32_t offset = info[1].ToNumber();
uint32_t length = info[2].ToNumber();
Expand Down Expand Up @@ -141,13 +133,11 @@ class Memif : public Napi::ObjectWrap<Memif>
m_nTxFragments += nAlloc;
}

void close(const Napi::CallbackInfo& info)
{
void close(const Napi::CallbackInfo& info) {
stop();
}

static int handleControlFdUpdate(memif_fd_event_t fde, void* self0)
{
static int handleControlFdUpdate(memif_fd_event_t fde, void* self0) {
auto self = reinterpret_cast<Memif*>(self0);
if ((fde.type & MEMIF_FD_EVENT_DEL) != 0) {
self->m_uvPolls.erase(fde.fd);
Expand All @@ -172,8 +162,7 @@ class Memif : public Napi::ObjectWrap<Memif>
return uv_poll_start(&ptr->handle, uvEvents, handlePoll);
}

static void handlePoll(uv_poll_t* handle, int status, int events)
{
static void handlePoll(uv_poll_t* handle, int status, int events) {
UvPoll& poll = UvPoll::of(handle);
int memifEvents = 0;
if (status < 0) {
Expand All @@ -189,23 +178,20 @@ class Memif : public Napi::ObjectWrap<Memif>
memif_control_fd_handler(poll.private_ctx, static_cast<memif_fd_event_type_t>(memifEvents));
}

static int handleConnect(memif_conn_handle_t conn, void* self0)
{
static int handleConnect(memif_conn_handle_t conn, void* self0) {
auto self = reinterpret_cast<Memif*>(self0);
memif_refill_queue(conn, 0, -1, 0);
self->setState(true);
return 0;
}

static int handleDisconnect(memif_conn_handle_t conn, void* self0)
{
static int handleDisconnect(memif_conn_handle_t conn, void* self0) {
auto self = reinterpret_cast<Memif*>(self0);
self->setState(false);
return 0;
}

static int handleInterrupt(memif_conn_handle_t conn, void* self0, uint16_t qid)
{
static int handleInterrupt(memif_conn_handle_t conn, void* self0, uint16_t qid) {
auto self = reinterpret_cast<Memif*>(self0);
if (!self->m_running) {
return 0;
Expand All @@ -223,36 +209,33 @@ class Memif : public Napi::ObjectWrap<Memif>
return 0;
}

void receive(const memif_buffer_t& b)
{
void receive(const memif_buffer_t& b) {
auto env = Env();
Napi::HandleScope scope(env);
auto u8 = Napi::Uint8Array::New(env, b.len);
std::memcpy(u8.Data(), b.data, b.len);
bool hasNext = (b.flags & MEMIF_BUFFER_FLAG_NEXT) != 0;
auto hasNextB = Napi::Boolean::New(env, hasNext);
m_rx.Call({ u8, hasNextB });
m_rx.Call({u8, hasNextB});

if (!hasNext) {
++m_nRxPackets;
}
++m_nRxFragments;
}

void setState(bool up)
{
void setState(bool up) {
if (m_connected == up) {
return;
}
m_connected = up;

auto env = Env();
Napi::HandleScope scope(env);
m_state.Call({ Napi::Boolean::New(env, up) });
m_state.Call({Napi::Boolean::New(env, up)});
}

void stop()
{
void stop() {
if (m_running) {
m_running = false;
setState(false);
Expand All @@ -279,26 +262,22 @@ class Memif : public Napi::ObjectWrap<Memif>
}

private:
class UvPoll : public memif_fd_event_t
{
class UvPoll : public memif_fd_event_t {
public:
explicit UvPoll(memif_fd_event_t fde, Memif* owner)
: memif_fd_event_t(fde)
, owner(owner)
{
, owner(owner) {
struct uv_loop_s* loop = nullptr;
napi_get_uv_event_loop(owner->Env(), &loop);
uv_poll_init(loop, &handle, fd);
handle.data = this;
}

~UvPoll()
{
~UvPoll() {
uv_poll_stop(&handle);
}

static UvPoll& of(uv_poll_t* handle)
{
static UvPoll& of(uv_poll_t* handle) {
return *reinterpret_cast<UvPoll*>(handle->data);
}

Expand All @@ -325,8 +304,7 @@ class Memif : public Napi::ObjectWrap<Memif>
};

Napi::Object
Init(Napi::Env env, Napi::Object exports)
{
Init(Napi::Env env, Napi::Object exports) {
return Memif::Init(env, exports);
}

Expand Down
3 changes: 1 addition & 2 deletions test/main.js
@@ -1,7 +1,6 @@
#!/usr/bin/env node
import assert from "node:assert/strict";
import crypto from "node:crypto";
import path from "node:path";
import { setTimeout as delay } from "node:timers/promises";
import { fileURLToPath } from "node:url";

Expand All @@ -15,7 +14,7 @@ const tmpDir = tmp.dirSync({ unsafeCleanup: true });
process.on("beforeExit", tmpDir.removeCallback);

const socketName = `${tmpDir.name}/memif.sock`;
const helper = execaNode(path.resolve(path.dirname(fileURLToPath(import.meta.url)), "server.js"), [socketName], {
const helper = execaNode(fileURLToPath(new URL("server.js", import.meta.url)), [socketName], {
stdin: "ignore",
stdout: "inherit",
stderr: "inherit",
Expand Down

0 comments on commit 622d953

Please sign in to comment.