Giter Site home page Giter Site logo

tiny-js's Introduction

tiny-js

A simple single-file javascript interpreter written in C++.

Foreword

The original project has been developed by Gordon Williams. Credits are due to him.

Description

This project aims to be an extremely simple (~2000 line) JavaScript interpreter, meant for inclusion in applications that require a simple, familiar script language that can be included with no dependencies other than normal C++ libraries. It currently consists of two source files - one containing the interpreter, another containing built-in functions such as String.substring.

TinyJS is not designed to be fast or full-featured. However it is great for scripting simple behaviour, or loading & saving settings.

I make absolutely no guarantees that this is compliant to JavaScript/EcmaScript standard. In fact I am sure it isn't. However I welcome suggestions for changes that will bring it closer to compliance without overly complicating the code, or useful test cases to add to the test suite.

Currently TinyJS supports:

  • Variables, arrays, structures
  • JSON parsing and output
  • Functions
  • Calling C/C++ code from JavaScript
  • Objects with inheritance (not fully implemented)

Please see the Code Examples chapter for examples of code that works.

For a list of known issues, please see the comments at the top of the TinyJS.cpp file.

To download TinyJS, just get SVN and run the following command:

svn checkout http://tiny-js.googlecode.com/svn/trunk/ tiny-js-read-only

There is also the 42tiny-js branch. This is maintained by Armin and provides a more fully-featured JavaScript implementation.

svn checkout http://tiny-js.googlecode.com/svn/branches/42tiny-js 42tiny-js

TinyJS is released under an MIT licence.

Internal Structure

TinyJS uses a Recursive Descent Parser, so there is no parser generator required. It does not compile to an intermediate code, and instead executes directly from source code. This makes it quite fast for code that is executed infrequently, and slow for loops.

Variables, arrays and objects are stored in a simple linked list tree structure (42tiny-js uses a std::map). This is simple, but relatively slow for large structures or arrays.

JavaScript for Microcontrollers

If you're after JavaScript for microcontrollers, take a look at the Espruino JavaScript Interpreter. It is a complete re-write of TinyJS targeted at processors with extremely low RAM (8KiB or more). It is currently available for a range of STM32 ARM Microcontrollers.

We've just launched a KickStarter for a board with it pre-installed. If it completes successfully we'll be releasing all hardware and software as Open Source!

Code Examples

The Variable result evaluates to true for each of these examples.

// simple for loop
var a = 0;
for (var i=1;i<10;i++) a = a + i;
result = a==45;
// simple function
function add(x,y) { return x+y; }
result = add(3,6)==9;
// functions in variables using JSON-style initialisation
var bob = { add : function(x,y) { return x+y; } };

result = bob.add(3,6)==9;
a = 345;    // an "integer", although there is only one numeric type in JavaScript
b = 34.5;   // a floating-point number
c = 3.45e2; // another floating-point, equivalent to 345
d = 0377;   // an octal integer equal to 255
e = 0xFF;   // a hexadecimal integer equal to 255, digits represented by the letters A-F may be upper or lowercase

result = a==345 && b*10==345 && c==345 && d==255 && e==255;
// references for arrays
var a;
a[0] = 10;
a[1] = 22;
b = a;
b[0] = 5;
result = a[0]==5 && a[1]==22 && b[1]==22;
// references with functions
var a = 42;
var b;
b[0] = 43;

function foo(myarray) {
  myarray[0]++;
}

function bar(myvalue) {
  myvalue++;
}

foo(b);
bar(a);

result = a==42 && b[0]==44;
// built-in functions

foo = "foo bar stuff";
r = Math.rand();

parsed = Integer.parseInt("42");

aStr = "ABCD";
aChar = aStr.charAt(0);

obj1 = new Object();
obj1.food = "cake";
obj1.desert = "pie";

obj2 = obj1.clone();
obj2.food = "kittens";

result = foo.length()==13 && foo.indexOf("bar")==4 && foo.substring(8,13)=="stuff" && parsed==42 && 
         Integer.valueOf(aChar)==65 && obj1.food=="cake" && obj2.desert=="pie";

tiny-js's People

Contributors

marcolizza 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

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

tiny-js's Issues

CTinyJS not defined?

I have tried to compile TinyJS on my Mac, but got:

Script.cpp:50:5: error: use of undeclared identifier 'CTinyJS'; did you mean 'TinyJS'?
    CTinyJS *js = (CTinyJS*)userdata;
    ^~~~~~~
    TinyJS
./TinyJS_Functions.h:35:11: note: 'TinyJS' declared here
namespace TinyJS {
          ^
Script.cpp:50:5: error: unexpected namespace name 'TinyJS': expected expression
    CTinyJS *js = (CTinyJS*)userdata;
    ^
Script.cpp:50:14: error: use of undeclared identifier 'js'
    CTinyJS *js = (CTinyJS*)userdata;
             ^
Script.cpp:50:20: error: use of undeclared identifier 'CTinyJS'; did you mean 'TinyJS'?
    CTinyJS *js = (CTinyJS*)userdata;
                   ^~~~~~~
                   TinyJS
./TinyJS_Functions.h:35:11: note: 'TinyJS' declared here
namespace TinyJS {
          ^
Script.cpp:50:20: error: unexpected namespace name 'TinyJS': expected expression
    CTinyJS *js = (CTinyJS*)userdata;
                   ^
Script.cpp:50:28: error: expected expression
    CTinyJS *js = (CTinyJS*)userdata;
                           ^
Script.cpp:51:5: error: use of undeclared identifier 'js'
    js->root->trace(">  ");
    ^
7 errors generated.

Should I use -DCTinyJS=TinyJS, maybe?

Get name of a cloned objects definition

Is there a way to get the name of an object? For e.g if i create an object like this

var myBaseObject = {
    name: "Test",
    value: 3
};

and now i want to get the name of the object myBaseObject. How can i do that? For e.g i want to write a function to tell me the name of the object

var test = myBaseObject.clone();
var theName = test.getObjectName();

Afterwards the variable theName should output myBaseObjetct.

Is that possible?

String.split

auto jsEngine = make_unique<CTinyJS>();
registerFunctions(jsEngine.get());
string js = "String('ape katt').split(' ');";
jsEngine->execute(js);

==> Error Expecting 'String' to be a function

build error Script.cpp:50:5: CTinyJS was not declared in this scope

commit db198df

Error details (console output)
make g++ -c -g -Wall -rdynamic -D_DEBUG run_tests.cpp -o run_tests.o g++ -c -g -Wall -rdynamic -D_DEBUG TinyJS.cpp -o TinyJS.o g++ -c -g -Wall -rdynamic -D_DEBUG TinyJS_Functions.cpp -o TinyJS_Functions.o g++ -c -g -Wall -rdynamic -D_DEBUG TinyJS_MathFunctions.cpp -o TinyJS_MathFunctions.o g++ -g -rdynamic run_tests.o TinyJS.o TinyJS_Functions.o TinyJS_MathFunctions.o -o run_tests g++ -c -g -Wall -rdynamic -D_DEBUG Script.cpp -o Script.o Script.cpp: In function ‘void js_dump(TinyJS::Variable*, void*)’: Script.cpp:50:5: error: ‘CTinyJS’ was not declared in this scope CTinyJS *js = (CTinyJS*)userdata; ^~~~~~~ Script.cpp:50:14: error: ‘js’ was not declared in this scope CTinyJS *js = (CTinyJS*)userdata; ^~ Script.cpp:50:28: error: expected primary-expression before ‘)’ token CTinyJS *js = (CTinyJS*)userdata; ^ Makefile:21: recipe for target 'Script.o' failed make: *** [Script.o] Error 1

Solution
replace in file Script.cpp line 50
CTinyJS *js = (CTinyJS*)userdata;
with
TinyJS::Interpreter *js = (TinyJS::Interpreter*)userdata;

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.