Giter VIP home page Giter VIP logo

node-jvm's Introduction

node-jvm Build Status

Overview

node-jvm - jvm in pure node.js

Example

java

public class Main {
    public static long fib(int n) {
        if (n <= 1) return n;
        return fib(n-1) + fib(n-2);
    }
    
    public static void main(String[] args) {
        if (args.length == 0) {
                System.out.print("help: java Main.class {Number}");
                return;
        }
        
        int N = Integer.parseInt(args[0]);
        long start = System.currentTimeMillis();            
        System.out.format("Fibonacci from 1 to %s:\n", N);
        for (int i = 1; i <= N; i++) {
            System.out.println(i + ": " + fib(i));
        }
        long stop = System.currentTimeMillis();
        System.out.println("time: " + (stop - start) + "ms");
        
        System.out.println("done.");
    }
}

node.js

var JVM = require("node-jvm");
var jvm = new JVM();
jvm.setLogLevel(7);
var entryPointClassName = jvm.loadJarFile("./Main.jar");
jvm.setEntryPointClassName(entryPointClassName);
jvm.on("exit", function(code) {
    process.exit(code);
});
jvm.run([15]);

build java files

cd examples/fibonacci; make

run jvm

./fibonacci.js

clean

make clean

output

Fibonacci from 1 to 15:
1: 1
2: 1
3: 2
4: 3
5: 5
6: 8
7: 13
8: 21
9: 34
10: 55
11: 89
12: 144
13: 233
14: 377
15: 610
time: 106ms
done.

other examples

cd examples/

arrays - working with different types of arrays 
dogs - simple object-oriented programming
fibonacci - recursion
jsclass - java and javascript mix
switcher - working with different switches
cast - cast for different types
ex - program exceptions
ex2 - jvm exceptions
idogs - working with interface
static - working with static objects
threads - multithreading

Developer

Yaroslav Gaponov (yaroslav.gaponov -at - gmail.com)

License

The MIT License (MIT)

Copyright (c) 2013 Yaroslav Gaponov

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

node-jvm's People

Contributors

alexgorbatchev avatar miktam avatar mlloreda avatar parroit avatar th3byrdm4n avatar yaroslavgaponov avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

node-jvm's Issues

error when run examples

ENV: node --version : v12.8.1

--------ERROR MESSAGES-----
./fibonacci.js

/Users/***/Labs/node-jvm/libs/logger.js:32
util.debug(msg);
^

TypeError: util.debug is not a function
at module.exports.Logger.debug (/Users/argan/Labs/node-jvm/libs/logger.js:32:14)
at module.exports.Classes.loadClassFile (/Users/argan/Labs/node-jvm/libs/classes.js:53:9)
at module.exports.JVM.loadClassFile (/Users/argan/Labs/node-jvm/libs/jvm.js:56:20)
at /Users/argan/Labs/node-jvm/libs/jvm.js:68:22
at Array.forEach ()
at module.exports.JVM.loadClassFiles (/Users/argan/Labs/node-jvm/libs/jvm.js:63:11)
at Object. (/Users/argan/Labs/node-jvm/examples/fibonacci/fibonacci.js:6:5)
at Module._compile (internal/modules/cjs/loader.js:868:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:879:10)
at Module.load (internal/modules/cjs/loader.js:731:32)

to run an error

internal/process/warning.js:18 (node:94662) [DEP0026] DeprecationWarning: util.print is deprecated. Use console.log instead.

Changes in Loader.prototype.createNewObject have broken tests

It appear that the offending change is the use of ctor.proto at line 139.
Created objects no more inherits methods from their parents. This make the mocha tests fails.

This could be easily corrected using prototype instead of proto. I admit that I don't know the difference between the two...

Any resources you referred ?

HI,

Great work ! I have got interested in the JVM areas recently as I am doing JAVA as part of my job nowadays. Any resources you can share that you referred along with your journey to writing a JVM would be helpful ! I would live to dive into the code.

Thanks :)

Can I ask you "why"?

Hi,

let me say I think your project is awesome!
You are top-notch!!!

But I'm very curious: what's the idea behind this?
Also, have you done any timing comparison with the Standard Oracle/Sun JVM?

Keep it up
Ivan

fibonacci.js example Range Error: Maximum call stack size exceeeded

โžœ fibonacci git:(master) โœ— ./fibonacci.js
DEBUG: JVM: loading ./Main.class ...
DEBUG: Not support opcodes: putstatic,athrow,checkcast,monitorenter,monitorexit,wide
Fibonacci from 1 to 10:
1: 1
2: 1
3: 2
4: 3
5: 5
6: 8
(node) warning: Recursive process.nextTick detected. This will break in the next version of node. Please use setImmediate for recursive deferral.
(node) warning: Recursive process.nextTick detected. This will break in the next version of node. Please use setImmediate for recursive deferral.
...
RangeError: Maximum call stack size exceeded

I get this same error when passing any int greater than 6 to Main.class in fibonacci.js. I'm using node v0.10.21.

fileName not defined

DEBUG: loading ./stagger.jar@se/su/ling/stagger/CTBTagger.class ...
\stagger\node_modules\node-jvm\libs\classes.js:52
            this.loadClassFile(path.dirname(fileName) + path.sep + classes[i] + ".class");
                                            ^

ReferenceError: fileName is not defined
    at Classes.loadClassBytes (\stagger\node_modules\node-jvm\libs\classes.js:52:45)
    at \stagger\node_modules\node-jvm\libs\classes.js:85:22
    at Array.forEach (native)
    at Classes.loadJarFile (\stagger\node_modules\node-jvm\libs\classes.js:81:16)
    at JVM.loadJarFile (\stagger\node_modules\node-jvm\libs\jvm.js:81:20)
    at Object.<anonymous> (\stagger\run.js:4:31)
    at Module._compile (module.js:434:26)
    at Object.Module._extensions..js (module.js:452:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)

TypeError [ERR_UNKNOWN_ENCODING]: Unknown encoding: null

I'm getting this error, I'm not sure if it's a problem with me or my jar file. I can't modify the file since it's downloaded from internet...

import JVM from "node-jvm";
import path from "path";

export class ServerJVM {
  #jvm;
  #entryPointClassName;

  constructor() {
    this.#jvm = new JVM();
    this.#jvm.setLogLevel(7);
    console.log(path.resolve("./server/server.jar"));
    this.#entryPointClassName = this.#jvm.loadJarFile(path.resolve("./server/server.jar"), "UTF-8");
    this.#jvm.setEntryPointClassName(this.#entryPointClassName);
  }

  async startServer({ onExit }) {
    this.#jvm.on("exit", function (code) {
      onExit(code);
    });
    this.#jvm.run([15]);
  }
}
node:internal/errors:496
    ErrorCaptureStackTrace(err);
    ^

TypeError [ERR_UNKNOWN_ENCODING]: Unknown encoding: null
    at new NodeError (node:internal/errors:405:5)
    at Buffer.toString (node:buffer:863:11)
    at Reader.readString (C:\Users\liamc\Programming\Javascript\mcm\puppet\node_modules\node-jvm\libs\util\reader.js:34:27)
    at getClassImage (C:\Users\liamc\Programming\Javascript\mcm\puppet\node_modules\node-jvm\libs\classfile\classarea.js:172:36)
    at new module.exports (C:\Users\liamc\Programming\Javascript\mcm\puppet\node_modules\node-jvm\libs\classfile\classarea.js:15:27)
    at Classes.loadClassBytes (C:\Users\liamc\Programming\Javascript\mcm\puppet\node_modules\node-jvm\libs\classes.js:47:21)
    at C:\Users\liamc\Programming\Javascript\mcm\puppet\node_modules\node-jvm\libs\classes.js:86:22
    at Array.forEach (<anonymous>)
    at Classes.loadJarFile (C:\Users\liamc\Programming\Javascript\mcm\puppet\node_modules\node-jvm\libs\classes.js:82:16)
    at JVM.loadJarFile (C:\Users\liamc\Programming\Javascript\mcm\puppet\node_modules\node-jvm\libs\jvm.js:81:20) {
  code: 'ERR_UNKNOWN_ENCODING'
}

Node.js v18.18.2

Supported JVM version?

Which version of the JVM does node-jvm support and does this package pass the Oracle Technology Compatibility Kit?

Hmmm...

Wow! This might have been a really nice doctorate research or something. But I will still stick with a C++ based JVM implementation.

Running jar file : RangeError: Trying to access beyond buffer length

Hi,

I tried to use the following java code :
https://github.com/tcurdt/lzo-index/tree/master
(complied jar file with maven)

I wanted to use it like this :

var JVM = require("node-jvm");
var jvm = new JVM();
jvm.setLogLevel(7);
var entryPointClassName = jvm.loadJarFile("./lzo-index-0.1.jar");
jvm.setEntryPointClassName(entryPointClassName);
jvm.on("exit", function(code) {
process.exit(code);
});
jvm.run(["file.lzo"]);

I got the following error :

DEBUG: loading ./lzo-index-0.1.jar@org/vafer/lzo/Main.class ...

/home/raru/dev/js/test/node_modules/node-jvm/libs/util/reader.js:0

^
RangeError: Trying to access beyond buffer length
at checkOffset (buffer.js:582:11)
at Buffer.readUInt16BE (buffer.js:616:5)
at Reader.read16 (/home/raru/dev/js/test/node_modules/node-jvm/libs/util/reader.js:22:27)
at getClassImage (/home/raru/dev/js/test/node_modules/node-jvm/libs/classfile/classarea.js:252:46)
at new module.exports (/home/raru/dev/js/test/node_modules/node-jvm/libs/classfile/classarea.js:15:27)
at Classes.loadClassBytes (/home/raru/dev/js/test/node_modules/node-jvm/libs/classes.js:47:21)
at /home/raru/dev/js/test/node_modules/node-jvm/libs/classes.js:85:22
at Array.forEach (native)
at Classes.loadJarFile (/home/raru/dev/js/test/node_modules/node-jvm/libs/classes.js:81:16)
at JVM.loadJarFile (/home/raru/dev/js/test/node_modules/node-jvm/libs/jvm.js:81:20)
at Object. (/home/raru/dev/js/test/java/java.js:5:31)
at Module._compile (module.js:456:26)

Does the support for jar file depends on how you compile it ?

"not support constant type" error

The following throws the "not support constant type" error:

for (int i = 0; i < 100000; i++) {
.....
}

inside the ldc in frame.js (line 213).

When I change it to for (long i = 0; i < 100000; i++), it works. Seems that ldc doesn't support int typed constants.

Mini-Micro-Optimization

Not important, but just catched my eyes: In libs/opcodes.js, the line

if (this._cache[opcode]) {

should probably read

if (opcode in this._cache) {

Otherwise, the cache is not used for nop's (opcode == 0x00).

Splitting out

Are you planning on splitting this into multiple modules? I would like to use bits and pieces of this for some Java bytecode parsing tools

ReferenceError: fileName is not defined

The following variables fileName cannot access, is bug?

Classes.prototype.loadClassBytes = function(bytes) {
    var classArea = new ClassArea(bytes);
    this.classes[classArea.getClassName()] = classArea;
    var classes = classArea.getClasses();
    for (var i=0; i<classes.length; i++) {
        if (!this.classes[classes[i]]) {
            this.loadClassFile(path.dirname(fileName) + path.sep + classes[i] + ".class");
        }
    }
    return classArea;
}

Browserify

Without looking too far into the details, it's not clear to me why this implementation should be limited to node. What shims, if any, would be needed to make it browserify compatible? Just adm-zip?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.