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

@gulg/mcjs

v1.0.0

Published

JavaScript to Minecraft datapacks compiler

Downloads

4

Readme

MCJS is a JavaScript to Minecraft Datapack compiler. It takes your JavaScript code, turns it into AST, turns that AST into MC commands and then injects the MCJS internals. It outputs a datapack, that you can add to your world without any modifications.

Quick start

Install MCJS through NPM:

npm i -g mcjs

Then, create a simple configuration file:

module.exports = {
    datapack: {
        name: "Example DataPack",
        namespace: "example",
        description: "Written in JS!",
        format: 48
    },
    include: ["./src/index.js"],
    output: "./dist"
};

Great! Now you can create a simple script and build it using MCJS:

// src/index.js
console.log("Hello world!");
mcjs ./mcjs.config.js

This should create dist folder in your project directory, containing your datapack.

How does it work?

MCJS uses Acorn to parse your JS files. It then uses data storages to create virtual object system. Everything is made according to this spec, however some features may not be implemented or may be simplified due to Minecraft limitations. In addition to internal properties specified in ECMA, MCJS also provides [[ObjectKind]] property to determine the type of an object: UNDEFINED (-1), NULL (0), OBJECT (1), PROXY (2). It's used in mcjs:internal/accessors/... to make lookup faster. I also used V8 and Chakra source code in my development.

Internal structure

Functions

MCJS engine datapack consists of three parts: internal functions for built-in objects, internal functions for binding JS OOP model to Minecraft data storages and load function, which initializes the whole engine. Internal object functions folder (functions) contains subfolders for every built-in object (like Function) with the following structure: /prototype/... for instance methods, /... for static methods, /prototype/internal/... for internal instance methods and /internal/... for static internal methods. MCJS utilizes macros to access data dynamically.

Objects

Every object is stored in mcjs:storage memory.view. Each object has its unique id, which is later passed to different functions, allowing them to change the value by reference. Each object is defined as a compound tag containing these child tags: internal (to store internal methods & properties), descriptors (to store public methods & properties) and properties (duplicate of descriptors, needed to access object's keys dynamically). So, the following JavaScript object can be represented in MCJS like this:

{
    x: 1,
    y: { a: 1, b: 2, c: 3 }
}
data modify storage mcjs:storage memory.view.1 set value { a: memory.view.n1, b: memory.view.n2, c: memory.view.n3 }

Notice, that memory.view.1 is not { a: 1, b: 2, c: 3 }, but instead it contains references to those numbers.

Implemented features

  • Modules (except for import * as ...)
  • Objects (partially)
  • Functions
  • Binary operations
    • Addition
  • Logic operations
    • And
    • Or
    • Not

Engine specifics

To learn more info about the engine itself, check out this file. Execution contexts are simplified to just contain variable bindings.