Пример #1
0
static void scTrace(const CFunctionsScopePtr &c, void * userdata) {
	CTinyJS *js = (CTinyJS*)userdata;
	if(c->getArgumentsLength())
		c->getArgument(0)->trace();
	else
		js->getRoot()->trace("root");
}
Пример #2
0
bool
run_test (const char *filename)
{
  printf ("TEST %s ", filename);
  struct stat results;
  if (!stat (filename, &results) == 0)
    {
      printf ("Cannot stat file! '%s'\n", filename);
      return false;
    }
  int size = results.st_size;
  FILE *file = fopen (filename, "rb");

  /* if we open as text, the number of bytes read may be > the size we read */
  if (!file)
    {
      printf ("Unable to open file! '%s'\n", filename);
      return false;
    }
  char *buffer = new char[size + 1];
  long actualRead = fread (buffer, 1, size, file);
  buffer[actualRead] = 0;
  buffer[size] = 0;
  fclose (file);
  CTinyJS s;
  registerFunctions (&s);
  registerMathFunctions (&s);
  s.root->addChild ("result", new CScriptVar ("0", SCRIPTVAR_INTEGER));
  try
  {
    s.execute (buffer);
  } catch (CScriptException * e)
  {
    printf ("ERROR: %s\n", e->text.c_str ());
  } bool pass = s.root->getParameter ("result")->getBool ();
  if (pass){
    printf ("PASS\n");
  }
  else
  {
      char fn[64];
      sprintf (fn, "%s.fail.js", filename);
      FILE *f = fopen (fn, "wt");
      if (f)
      {
	  wString symbols;
	  s.root->getJSON (symbols);
	  fprintf (f, "%s", symbols.c_str ());
	  fclose (f);
      }
      printf ("FAIL - symbols written to %s\n", fn);
  }
  delete[] buffer;
  return pass;
}
Пример #3
0
int main2(int argc, char **argv)
{
  CTinyJS *js = new CTinyJS();
  /* add the functions from TinyJS_Functions.cpp */
  registerFunctions(js);
  /* Add a native function */
  js->addNative("function print(text)", &js_print, 0);
  js->addNative("function dump()", &js_dump, js);
  /* Execute out bit of code - we could call 'evaluate' here if
     we wanted something returned */
  try {
    js->execute("var lets_quit = 0; function quit() { lets_quit = 1; }");
    js->execute("print(\"Interactive mode... Type quit(); to exit, or print(...); to print something, or dump() to dump the symbol table!\");");
  } catch (CScriptException *e) {
    printf("ERROR: %s\n", e->text.c_str());
  }

  while (js->evaluate("lets_quit") == "0") {
    char buffer[2048];
    fgets ( buffer, sizeof(buffer), stdin );
    try {
      js->execute(buffer);
    } catch (CScriptException *e) {
      printf("ERROR: %s\n", e->text.c_str());
    }
  }
  delete js;
#ifdef _WIN32
#ifdef _DEBUG
  _CrtDumpMemoryLeaks();
#endif
#endif
  return 0;
}
Пример #4
0
int main(int , char **)
{
	CTinyJS *js = new CTinyJS();
	/* add the functions from TinyJS_Functions.cpp */
	registerFunctions(js);
	registerStringFunctions(js);
	registerMathFunctions(js);
	/* Add a native function */
	js->addNative("function print(text)", &js_print, 0);
//  js->addNative("function dump()", &js_dump, js);
	/* Execute out bit of code - we could call 'evaluate' here if
		we wanted something returned */
	try {
		js->execute("var lets_quit = 0; function quit() { lets_quit = 1; }");
		js->execute("print(\"Interactive mode... Type quit(); to exit, or print(...); to print something, or dump() to dump the symbol table!\");");
	} catch (CScriptException *e) {
		printf("%s\n", e->toString().c_str());
		delete e;
	}
	int lineNumber = 0;
	while (js->evaluate("lets_quit") == "0") {
		char buffer[2048];
		fgets ( buffer, sizeof(buffer), stdin );
		try {
			js->execute(buffer, "console.input", lineNumber++);
		} catch (CScriptException *e) {
			printf("%s\n", e->toString().c_str());
			delete e;
		}
	}
	delete js;
#ifdef _WIN32
#ifdef _DEBUG
//  _CrtDumpMemoryLeaks();
/*
	no dump momoryleaks here
	_CrtSetDbgFlag(..) force dump memoryleake after call of all global deconstructors
*/
	_CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
#endif
#endif
	return 0;
}
Пример #5
0
int main(int , char **)
{
	char dummy;
	topOfStack = &dummy;
//	printf("%i %i\n", __cplusplus, _MSC_VER);

//	printf("Locale:%s\n",setlocale( LC_ALL, 0 ));
//	setlocale( LC_ALL, ".858" );
//	printf("Locale:%s\n",setlocale( LC_ALL, 0 ));
	CTinyJS *js = new CTinyJS();
	/* add the functions from TinyJS_Functions.cpp */
//	registerFunctions(js);
//	registerStringFunctions(js);
//	registerMathFunctions(js);
	/* Add a native function */
	js->addNative("function print(text)", &js_print, 0);
//  js->addNative("function dump()", &js_dump, js);
	/* Execute out bit of code - we could call 'evaluate' here if
		we wanted something returned */
	js->setStackBase(topOfStack-(sizeOfStack-sizeOfSafeStack));
	try {
		js->execute("var lets_quit = 0; function quit() { lets_quit = 1; }");
		js->execute("print(\"Interactive mode... Type quit(); to exit, or print(...); to print something, or dump() to dump the symbol table!\");");
		js->execute("print(function () {print(\"gen\");yield 5;yield 6;}().next());", "yield-test.js");
		js->execute("for each(i in function () {print(\"gen\");yield 5;yield 6;}()) print(i);", "yield-test.js");
		js->execute("function g(){				\n\n"
			"	throw \"error\"\n"
			"	try{									\n"
			"		yield 1; yield 2				\n"
			"	}finally{							\n"
			"		print(\"finally\")			\n"
			"		yield 3;							\n"
			"		throw StopIteration			\n"
			"	}										\n"
			"	print(\"after finally\")		\n"
			"}t=g()", "test");
	} catch (CScriptException &e) {
		printf("%s\n", e.toString().c_str());
	}
	int lineNumber = 0;
	while (js->evaluate("lets_quit") == "0") {
		std::string buffer;
		if(!std::getline(std::cin, buffer)) break;
		try {
			js->execute(buffer, "console.input", lineNumber++);
		} catch (CScriptException &e) {
			printf("%s\n", e.toString().c_str());
		}
	}
	delete js;
#ifdef _WIN32
#ifdef _DEBUG
//  _CrtDumpMemoryLeaks();
/*
	no dump momoryleaks here
	_CrtSetDbgFlag(..) force dump memoryleake after call of all global deconstructors
*/
	_CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
#endif
#endif
	return 0;
}
Пример #6
0
void scEval(CScriptVar *c, void *data) {
    CTinyJS *tinyJS = (CTinyJS *)data;
    std::string str = c->getParameter("jsCode")->getString();
    c->setReturnVar(tinyJS->evaluateComplex(str).var);
}
Пример #7
0
void scExec(CScriptVar *c, void *data) {
    CTinyJS *tinyJS = (CTinyJS *)data;
    std::string str = c->getParameter("jsCode")->getString();
    tinyJS->execute(str);
}
Пример #8
0
void scExec(CScriptVar *c, void *userdata) {
    IGNORE_PARAMETER(userdata);
    CTinyJS *tinyJS = (CTinyJS *)userdata;
    wString str = c->getParameter("jsCode")->getString();
    tinyJS->execute(str);
}