npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

libclangjs

v0.5.1

Published

JavaScript bindings to the libClang API via WebAssembly and Emscripten.

Downloads

66

Readme

Release libclangjs

Quickstart

1. Install the package from NPM

$ npm install libclangjs

2.a. Usage with NodeJS

Add the Code you want to analyze using clang

// main.cpp in the root of your project, alongside the package.json
class TestClass {
public:
  TestClass();
  ~TestClass();
};

Analyze the code using clang

import init from "libclangjs/node.js"; // The NodeJS version of libclang uses the CommonJS module format

const cwd = "/home/web_user";

init().then(clang => {
  clang.FS.mount(clang.NODEFS, { root: "." }, cwd); // Share the current directory with libclang
  const index = clang.createIndex(1, 1);
  const tu = clang.parseTranslationUnit(index, `${cwd}/main.cpp`, null, null, 0);
  const cursor = clang.getTranslationUnitCursor(tu);

  clang.visitChildren(cursor, (c, p) => {
    console.log(clang.getCursorKindSpelling(clang.getCursorKind(c)), clang.getCursorSpelling(c), clang.getPresumedLocation(clang.getCursorLocation(c)));
    return clang.CXChildVisitResult.Recurse;
  });

  clang.PThread.terminateAllThreads();
});

2.b. Usage with the browser

const code =
`class TestClass {
public:
  TestClass();
  ~TestClass();
};
`;

// ...

import init from "libclangjs/web"; // The web version of libclang uses the ESM module format

const cwd = "/home/web_user";
init().then(clang => {
  clang.FS.writeFile(`${cwd}/main.cpp`, code); // Create a source file in the memory file system (sharing is not possible, here)
  const index = clang.createIndex(1, 1);
  const tu = clang.parseTranslationUnit(index, `${cwd}/main.cpp`, null, null, 0);
  const cursor = clang.getTranslationUnitCursor(tu);

  clang.visitChildren(cursor, (c, p) => {
    console.log(clang.getCursorKindSpelling(clang.getCursorKind(c)), clang.getCursorSpelling(c), clang.getPresumedLocation(clang.getCursorLocation(c)));
    return clang.CXChildVisitResult.Recurse;
  });

  clang.PThread.terminateAllThreads();
});

Libclang uses multi-threading for maximum performance, which requires the use of a SharedArrayBuffer, which is a well supported feature in modern browsers. Enabling this feature requires setting the Cross-Origin-Opener-Policy: same-origin and Cross-Origin-Embedder-Policy: require-corp headers on the top level document, as described here. An example config for NextJS would look like this:

// next.config.js
const nextConfig = {
  headers: async () => [{
    source: "/:path*",
    headers: [{
      key: "Cross-Origin-Opener-Policy",
      value: "same-origin",
    }, {
      key: "Cross-Origin-Embedder-Policy",
      value: "require-corp",
    }],
  }],
}

module.exports = nextConfig

3. Output

ClassDecl TestClass { filename: '/home/web_user/main.cpp', line: 1, column: 7 }
CXXAccessSpecifier  { filename: '/home/web_user/main.cpp', line: 2, column: 1 }
CXXConstructor TestClass { filename: '/home/web_user/main.cpp', line: 3, column: 3 }
CXXDestructor ~TestClass { filename: '/home/web_user/main.cpp', line: 4, column: 3 }

The execution will take several seconds to complete. A big part of that time is taken by the call to init. Subsequent function calls should run fast - but so far no benchmarking against the native version has been done.

Extending / Integrating with other C/C++ code

Check out Libclangjs-cmake on NPM and this test for an example on how this can be achieved.

Contribution and release workflow

This project ist using Changesets for publishing and managing versions.

  1. Create a new branch / PR with the desired canges
  2. After the changes are made, run pnpm changeset and answer the questionnaire. Commit the results.