Permalink
Browse files

added js counterparts of the tests

  • Loading branch information...
1 parent 2458397 commit 20a392fbce7230c2b6db3223935fbf3ac8e59728 @vjeux committed Apr 23, 2011
Showing with 158 additions and 11 deletions.
  1. +13 −0 Makefile
  2. +0 −2 example/dynamic.cpp
  3. +11 −0 example/dynamic.js
  4. +1 −1 example/exception.cpp
  5. +12 −0 example/exception.js
  6. +19 −0 example/function.js
  7. +23 −0 example/iteration.js
  8. +16 −0 example/json.js
  9. +2 −2 example/reference.cpp
  10. +9 −0 example/reference.js
  11. +23 −0 example/scope.js
  12. +6 −6 example/this.cpp
  13. +23 −0 example/this.js
View
@@ -0,0 +1,13 @@
+
+CC = g++
+CFLAGS = -Wall -std=gnu++0x
+FILES = $(wildcard example/*.cpp)
+TARGETS = $(FILES:.cpp=.jspp)
+
+all: $(TARGETS)
+
+%.jspp: %.cpp
+ $(CC) -o $@ $< $(CFLAGS)
+
+clean:
+ rm $(TARGETS)
View
@@ -1,5 +1,3 @@
-#include <iostream>
-
#include "../src/javascript_start.h"
var repeat = function (var str, var times) {
View
@@ -0,0 +1,11 @@
+
+var repeat = function (str, times) {
+ var ret = "";
+ for (var i = 0; i < times; ++i) {
+ ret += str + i;
+ }
+ return ret;
+};
+
+console.log(repeat(" js++", 3));
+// " js++0 js++1 js++2"
View
@@ -9,6 +9,6 @@ try {
} catch (var e) {
std::cout << "Error: " << e << std::endl;
}
-// Error: Exception
+// Error: Exception!
#include "../src/javascript_end.h"
View
@@ -0,0 +1,12 @@
+
+var go_die = function () {
+ throw "Exception!";
+};
+
+try {
+ go_die();
+} catch (e) {
+ console.log("Error:", e);
+}
+// Error: Exception!
+
View
@@ -0,0 +1,19 @@
+
+var Utils = {
+ "map": function (array, func) {
+ for (var i = 0; i < array["length"]; ++i) {
+ array[i] = func(i, array[i]);
+ }
+ }
+};
+
+var a = ["a", "b", "c"];
+console.log(a);
+// [a, b, c]
+
+Utils["map"](a, function (key, value) {
+ return "(" + key + ":" + value + ")";
+});
+console.log(a);
+// [(0:a), (1:b), (2:c)]
+
View
@@ -0,0 +1,23 @@
+
+var array = [10, 42, 30];
+for (var i in array) {
+ console.log(i, array[i]);
+}
+// 0 - 10
+// 1 - 42
+// 2 - 30
+
+
+
+var object = {
+ "a": 1,
+ "b": 2,
+ "c": 3
+};
+for (var i in object) {
+ console.log(i, object[i]);
+}
+// a - 1
+// b - 2
+// c - 3
+
View
@@ -0,0 +1,16 @@
+
+var json = {
+ "number": 42,
+ "string": "kikoo",
+ "array": [1, 2, "lol"],
+
+ "nested": {
+ "first": 1
+ }
+};
+
+console.log(json);
+// { number: 42,
+// string: 'kikoo',
+// array: [ 1, 2, 'lol' ],
+// nested: { first: 1 } }
View
@@ -1,13 +1,13 @@
#include "../src/javascript_start.h"
-var a = 0;
+var a = {};
a["key"] = "old";
var b = a;
b["key"] = "new";
-std::cout << a["key"] << " - " << b["key"] << std::endl;
+std::cout << a["key"] << " " << b["key"] << std::endl;
// new - new
View
@@ -0,0 +1,9 @@
+
+var a = {};
+a["key"] = "old";
+
+var b = a;
+b["key"] = "new";
+
+console.log(a["key"], b["key"]);
+// new - new
View
@@ -0,0 +1,23 @@
+
+var global = "global";
+var $ = "prototype";
+var jQuery = "jQuery";
+
+(function ($) {
+ var global = "local";
+
+ console.log("Inside: $ = ", $);
+ console.log("Inside: global = ", global);
+
+ // Inside: $ = jQuery
+ // Inside: global = local
+
+ return undefined;
+})(jQuery);
+
+console.log("Outside: $ = ", $);
+console.log("Outside: global = ", global);
+
+// Outside: $ = prototype
+// Outside: global = global
+
View
@@ -1,27 +1,27 @@
#include "../src/javascript_start.h"
var f = function (var x, var y) {
- std::cout << "This: " << This << std::endl;
+ std::cout << "this: " << This << std::endl;
This["x"] = x;
This["y"] = y;
return This;
};
// New creates a new object this
-var a = New(f)(1, 2); // This: <function 0x005240d0>
-var b = New(f)(3, 4); // This: <function 0x005248e0>
+var a = New(f)(1, 2); // this: <function 0x005240d0>
+var b = New(f)(3, 4); // this: <function 0x005248e0>
// Unbound call,
-var c = f(5, 6); // This: undefined
+var c = f(5, 6); // this: undefined
// Bound call
var obj = {42};
obj["f"] = f;
-var d = obj["f"](1, 2); // This: [42]
+var d = obj["f"](1, 2); // this: [42]
// Call
-var e = f["call"](obj, 1, 2); // This: [42]
+var e = f["call"](obj, 1, 2); // this: [42]
#include "../src/javascript_end.h"
View
@@ -0,0 +1,23 @@
+
+var f = function (x, y) {
+ console.log("this:", this);
+ this["x"] = x;
+ this["y"] = y;
+};
+
+// New creates a new object this
+var a = new f(1, 2); // this: <object 0x005240d0>
+var b = new f(3, 4); // this: <object 0x005248e0>
+
+// Unbound call,
+var c = f(5, 6); // this: global object
+
+// Bound call
+var obj = [42];
+obj["f"] = f;
+
+var d = obj["f"](1, 2); // this: [42]
+
+// Call
+var e = f["call"](obj, 1, 2); // this: [42]
+

0 comments on commit 20a392f

Please sign in to comment.