Giter Club home page Giter Club logo

motto's Introduction

motto

Build Status

Modular otto

Motto provide a Nodejs like module environment to run javascript files in golang.

Installation

go get github.com/ddliu/motto

Usage

var _ = require('underscore');
var data = require('./data.json'); // [3, 2, 1, 4, 6]
module.exports = _.min(data);
package main

import (
    "github.com/ddliu/motto"
    _ "github.com/ddliu/motto/underscore"
)

func main() {
    motto.Run("path/to/index.js")
}

You can also install the motto command line tool to run it directly:

go install github.com/ddliu/motto/motto
motto path/to/index.js

Modules

The module environment is almost capable with Nodejs spec.

Some Nodejs modules(without core module dependencies) can be used directly in Motto.

Addons

Motto can be extended with addons, below is an example addon which implement part of the "fs" module of Nodejs:

package fs

import (
    "github.com/ddliu/motto"
    "github.com/robertkrimen/otto"
)

func fsModuleLoader(vm *motto.Motto) (otto.Value, error) {
    fs, _ := vm.Object(`({})`)
    fs.Set("readFileSync", func(call otto.FunctionCall) otto.Value {
        filename, _ := call.Argument(0).ToString()
        bytes, err := ioutil.ReadFile(filename)
        if err != nil {
            return otto.UndefinedValue()
        }

        v, _ := call.Otto.ToValue(string(bytes))
        return v
    })

    return vm.ToValue(fs)
}

func init() {
    motto.AddModule("fs", fsModuleLoader)
}

After import this package, you can require it directly in your js file:

var fs = require('fs');
var content = fs.readFileSync('/path/to/data');

Nodejs in Golang?

nodego implements some features and core modules of Nodejs.

Performance

Simple benchmark shows below for furthur performance optimize:

strace -c -Ttt motto tests/index.js
strace -c -Ttt node tests/index.js

Motto:

% time     seconds  usecs/call     calls    errors syscall
------ ----------- ----------- --------- --------- ----------------
 20.20    0.000144           2        59           rt_sigaction
 15.71    0.000112           7        15           mmap
 11.92    0.000085          11         8           futex
 10.10    0.000072           6        13         4 stat
  7.43    0.000053           7         8           read
  5.89    0.000042          21         2           clone
  5.75    0.000041          10         4           open
  4.77    0.000034           7         5           rt_sigprocmask
  4.63    0.000033          33         1           execve
  3.23    0.000023          12         2           write
  2.24    0.000016           4         4           fstat
  1.82    0.000013           3         4           close
  1.82    0.000013          13         1           sched_getaffinity
  1.68    0.000012          12         1           sched_yield
  0.98    0.000007           7         1           munmap
  0.98    0.000007           7         1           arch_prctl
  0.42    0.000003           3         1           getcwd
  0.42    0.000003           3         1           sigaltstack
------ ----------- ----------- --------- --------- ----------------
100.00    0.000713                   131         4 total

Nodejs:

% time     seconds  usecs/call     calls    errors syscall
------ ----------- ----------- --------- --------- ----------------
 20.15    0.000636           7        92           mmap
 17.78    0.000561          16        36           munmap
 13.97    0.000441          18        24           read
  7.73    0.000244           7        35           mprotect
  7.70    0.000243          15        16           brk
  7.32    0.000231           7        34         1 futex
  4.56    0.000144           7        22           open
  3.61    0.000114           5        22        15 ioctl
  2.15    0.000068           3        21           close
  2.03    0.000064           3        21           fstat
  2.00    0.000063           5        14        14 access
  1.58    0.000050           4        12           lstat
  1.24    0.000039           8         5           write
  1.24    0.000039           6         7           rt_sigaction
  1.05    0.000033           6         6         2 stat
  0.89    0.000028          28         1           readlink
  0.76    0.000024           5         5           rt_sigprocmask
  0.70    0.000022           6         4           getcwd
  0.67    0.000021          11         2           pipe2
  0.63    0.000020          20         1           clone
  0.38    0.000012           6         2           getrlimit
  0.29    0.000009           9         1           epoll_create1
  0.25    0.000008           8         1           eventfd2
  0.22    0.000007           7         1           clock_gettime
  0.22    0.000007           4         2           epoll_ctl
  0.19    0.000006           6         1           gettid
  0.19    0.000006           6         1           set_tid_address
  0.19    0.000006           6         1           set_robust_list
  0.13    0.000004           4         1           execve
  0.10    0.000003           3         1           arch_prctl
  0.10    0.000003           3         1           epoll_wait
------ ----------- ----------- --------- --------- ----------------
100.00    0.003156                   393        32 total

Changelog

v0.1.0 (2014-06-22)

Initial release

v0.2.0 (2014-06-24)

Make module capable with Nodejs

v0.3.0 (2014-06-26)

Rewrite module.

Make it easier to write addons.

Add underscore addon as an example.

motto's People

Contributors

ddliu avatar gaubee avatar gernest avatar ialex32x avatar mmatczuk avatar vozhyk- 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

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

motto's Issues

Hangs while searching for module

I am trying to use an html to jsx converter(https://github.com/littlehaker/html-to-jsx) with motto. I'm trying to run the example code within motto but code keeps looking for a certain "util.js" file.

  • Ran npm init
  • npm install --save html-to-jsx
  • I copied the htmltojsx/example.js to a file called html2jsx.js in my app directory
  • Created the following test in main_test.go
  • Run go test -run=TestNodeIntegration
func TestNodeIntegration(t *testing.T) {

	m := motto.New()
	m.AddPath("node_modules")
	m.AddPath("node_modules/util")
	v, err := m.Run("html2jsx.js")
	if err != nil {
		t.Error(err)
	}

	i, _ := v.ToString()
	fmt.Println(i)
}

I added the following log statement in motto/module.go
Line 128: fmt.Println(name, choices)

Running the test yielded the following code repeated over and over.

util [/Users/alex/projects/go/src/com.abc/transpileangulartomithril/node_modules/util/node_modules/inherits/node_modules/util.js /Users/alex/projects/go/src/com.abc/transpileangulartomithril/node_modules/util/node_modules/inherits/node_modules/util.json /Users/alex/projects/go/src/com.abc/transpileangulartomithril/node_modules/util/node_modules/inherits/node_modules/util node_modules/util.js node_modules/util.json node_modules/util]
node_modules/util/util.js [node_modules/util/util.js]
/Users/alex/projects/go/src/com.abc/transpileangulartomithril/node_modules/util/support/isBuffer [/Users/alex/projects/go/src/com.abc/transpileangulartomithril/node_modules/util/support/isBuffer.js /Users/alex/projects/go/src/com.abc/transpileangulartomithril/node_modules/util/support/isBuffer.json /Users/alex/projects/go/src/com.abc/transpileangulartomithril/node_modules/util/support/isBuffer]
inherits [/Users/alex/projects/go/src/com.abc/transpileangulartomithril/node_modules/util/node_modules/inherits.js /Users/alex/projects/go/src/com.abc/transpileangulartomithril/node_modules/util/node_modules/inherits.json /Users/alex/projects/go/src/com.abc/transpileangulartomithril/node_modules/util/node_modules/inherits node_modules/inherits.js node_modules/inherits.json node_modules/inherits]
/Users/alex/projects/go/src/com.abc/transpileangulartomithril/node_modules/util/node_modules/inherits/inherits.js [/Users/alex/projects/go/src/com.abc/transpileangulartomithril/node_modules/util/node_modules/inherits/inherits.js]
util [/Users/alex/projects/go/src/com.abc/transpileangulartomithril/node_modules/util/node_modules/inherits/node_modules/util.js /Users/alex/projects/go/src/com.abc/transpileangulartomithril/node_modules/util/node_modules/inherits/node_modules/util.json /Users/alex/projects/go/src/com.abc/transpileangulartomithril/node_modules/util/node_modules/inherits/node_modules/util node_modules/util.js node_modules/util.json node_modules/util]
node_modules/util/util.js [node_modules/util/util.js]

Can't find exactly where it is trying to load util. I tried running npm install util but that did not change anything. I was Curious if it is trying to load node module. I saw nodego but could not figure out how to use the nodego environment within motto.

Loading module multiple times is not idempotent

Code as follows

func TestDoubleLoadModule(t *testing.T) {
	loader := CreateLoaderFromSource(`{
		module.exports = "bar";
	}`, "")

	testLoad := func() {
		vm := New()
		vm.AddModule("foo", loader)
		v, err := vm.Require("foo", ".")
		if err != nil {
			t.Fatal(err)
		}
		s, _ := v.ToString()
		if s != "bar" {
			t.Errorf("expect bar, got %s", s)
		}
	}
	testLoad()
	testLoad()
}

Output of test

--- FAIL: TestDoubleLoadModule (0.00s)
    motto_test.go:85: expect bar, got [object Object]
FAIL

The reason is that multiple calls to ModuleLoader cause the source to be repeatedly assigned.

source = "(function(module) {var require = module.require;var exports = module.exports;var __dirname = module.__dirname;" + source + "\n})"

TestNpmModule Fails

TestNpmModule fails, at least on macOS 10.12.4. go1.6.4.

motto $ go test
load index.js
load sort.js
--- FAIL: TestNpmModule (0.00s)
        motto_test.go:29: Error: motto: Module not found: underscore
        motto_test.go:35: npm test failed:  0 != 1
FAIL
exit status 1
FAIL    github.com/ddliu/motto  0.035s

Looking at the .js file:

var _ = require('underscore');
module.exports =  _.min([3,2,1,4,5]);

My understanding of this is that 'underscore' is expected to be in node_modules, but no such directory exists. The test data seems incomplete?

Dependencies of modules in node_modules can't be loaded

Motto is unable to load any module from node_modules which requires other modules in node_modules. So if I require('foo') and the foo module in turn will require('bar'), then motto fails saying that 'bar' cannot be found.

Motto seems to be searching for node_modules/foo/node_modules/bar, which is of course incorrect.

N.B. I don't think this is related to gh-10 as it this looks to be a bug in motto itself, while that issue is about missing/incorrect test data.

Requiring node modules

I am trying to render reactjs on the server using otto. My entry point is called app.js and requires modules installed through npm, who also have dependencies in node_modules. Is there a straight forward way to run app.js in motto with all of its node dependencies?

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.