/*
* TinyJS
*
* A single-file Javascript-alike engine
*
* Authored By Gordon Williams <[email protected]>
*
* Copyright (C) 2009 Pur3 Ltd
*
* 42TinyJS
*
* A fork of TinyJS with the goal to makes a more JavaScript/ECMA compliant engine
*
* Authored / Changed By Armin Diedering <[email protected]>
*
* Copyright (C) 2010-2014 ardisoft
*
*
* 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.
*/
#ifdef _DEBUG
# ifndef _MSC_VER
# define DEBUG_MEMORY 1
# endif
#endif
#include <errno.h>
#include <sstream>
#include <fstream>
#include "TinyJS.h"
#ifndef ASSERT
# define ASSERT(X) assert(X)
#endif
#ifndef NO_REGEXP
# if defined HAVE_TR1_REGEX
# include <tr1/regex>
using namespace std::tr1;
# elif defined HAVE_BOOST_REGEX
# include <boost/regex.hpp>
using namespace boost;
# else
# include <regex>
# endif
#else
# include <algorithm>
# include <cmath>
# include <memory>
#endif
using namespace std;
// -----------------------------------------------------------------------------------
//////////////////////////////////////////////////////////////////////////
/// Memory Debug
//////////////////////////////////////////////////////////////////////////
//#define DEBUG_MEMORY 1
#if DEBUG_MEMORY
vector<CScriptVar*> allocatedVars;
vector<CScriptVarLink*> allocatedLinks;
void mark_allocated(CScriptVar *v) {
allocatedVars.push_back(v);
}
void mark_deallocated(CScriptVar *v) {
for (size_t i=0;i<allocatedVars.size();i++) {
if (allocatedVars[i] == v) {
allocatedVars.erase(allocatedVars.begin()+i);
break;
}
}
}
void mark_allocated(CScriptVarLink *v) {
allocatedLinks.push_back(v);
}
void mark_deallocated(CScriptVarLink *v) {
for (size_t i=0;i<allocatedLinks.size();i++) {
if (allocatedLinks[i] == v) {
allocatedLinks.erase(allocatedLinks.begin()+i);
break;
}
}
}
void show_allocated() {
for (size_t i=0;i<allocatedVars.size();i++) {
printf("ALLOCATED, %d refs\n", allocatedVars[i]->getRefs());
allocatedVars[i]->trace(" ");
}
for (size_t i=0;i<allocatedLinks.size();i++) {
printf("ALLOCATED LINK %s, allocated[%d] to \n", allocatedLinks[i]->getName().c_str(), allocatedLinks[i]->getVarPtr()->getRefs());
allocatedLinks[i]->getVarPtr()->trace(" ");
}
allocatedVars.clear();
allocatedLinks.clear();
}
#endif
//////////////////////////////////////////////////////////////////////////
/// Utils
//////////////////////////////////////////////////////////////////////////
inline bool isWhitespace(char ch) {
return (ch==' ') || (ch=='\t') || (ch=='\n') || (ch=='\r');
}
inline bool isNumeric(char ch) {
return (ch>='0') && (ch<='9');
}
uint32_t isArrayIndex(const string &str) {
if(str.size()==0 || !isNumeric(str[0]) || (str.size()>1 && str[0]=='0') ) return -1; // empty or (more as 1 digit and beginning with '0')
CNumber idx;
const char *endptr;
idx.parseInt(str.c_str(), 10, &endptr);
if(*endptr || idx>uint32_t(0xFFFFFFFFUL)) return -1;
return idx.toUInt32();
}
inline bool isHexadecimal(char ch) {
return ((ch>='0') && (ch<='9')) || ((ch>='a') && (ch<='f')) || ((ch>='A') && (ch<='F'));
}
inline bool isOctal(char ch) {
return ((ch>='0') && (ch<='7'));
}
inline bool isAlpha(char ch) {
return ((ch>='a') && (ch<='z')) || ((ch>='A') && (ch<='Z')) || ch=='_' || ch=='$';
}
bool isIDString(const char *s) {
if (!isAlpha(*s))
return false;
while (*s) {
if (!(isAlpha(*s) || isNumeric(*s)))
return false;
s++;
}
return true;
}
void replace(string &str, char textFrom, const char *textTo) {
int sLen = strlen(textTo);
size_t p = str.find(textFrom);
while (p != string::npos) {
str = str.substr(0, p) + textTo + str.substr(p+1);
p = str.find(textFrom, p+sLen);
}
}
string int2string(int32_t intData) {
ostringstream str;
str << intData;
return str.str();
}
string int2string(uint32_t intData) {
ostringstream str;
str << intData;
return str.str();
}
string float2string(const double &floatData) {
ostringstream str;
str.unsetf(ios::floatfield);
#if (defined(_MSC_VER) && _MSC_VER >= 1600) || __cplusplus >= 201103L
str.precision(numeric_limits<double>::max_digits10);
#else
str.precision(numeric_limits<double>::digits10+2);
#endif
str << floatData;
return str.str();
}
/// convert the given string into a quoted string suitable for javascript
string getJSString(const string &str) {
char buffer[5] = "\\x00";
string nStr; nStr.reserve(str.length());
nStr.push_back('\"');
for (string::const_iterator i=str.begin();i!=str.end();i++) {
const char *replaceWith = 0;
switch (*i) {
case '\\': replaceWith = "\\\\"; break;
case '\n': replaceWith = "\\n"; break;
case '\r': replaceWith = "\\r"; break;
case '\a': replaceWith = "\\a"; break;
case '\b': replaceWith = "\\b"; break;
case '\f': replaceWith = "\\f"; break;
case '\t': replaceWith = "\\t"; break;
case '\v': replaceWith = "\\v"; break;
case '"': replaceWith = "\\\""; break;
default: {
int nCh = ((unsigned char)*i) & 0xff;
if(nCh<32 || nCh>127) {
static char hex[] = "0123456789ABCDEF";
buffer[2] = hex[(nCh>>4)&0x0f];
buffer[3] = hex[nCh&0x0f];
replaceWith = buffer;
};
}
}
if (replaceWith)
nStr.append(replaceWith);
else
nStr.push_back(*i);
}
nStr.push_back('\"');
return nStr;
}
static inline string getIDString(const string& str) {
if(isIDString(str.c_str()) && CScriptToken::isReservedWord(str)==LEX_ID)
return str;
return getJSString(str);
}
//////////////////////////////////////////////////////////////////////////
/// CScriptException
//////////////////////////////////////////////////////////////////////////
string CScriptException::toString() {
ostringstream msg;
msg << ERROR_NAME[errorType] << ": " << message;
if(lineNumber >= 0) msg << " at Line:" << lineNumber+1;
if(column >=0) msg << " Column:" << column+1;
if(fileName.length()) msg << " in " << fileName;
return msg.str();
}
//////////////////////////////////////////////////////////////////////////
/// CScriptLex
//////////////////////////////////////////////////////////////////////////
CScriptLex::CScriptLex(const char *Code, const string &File, int Line, int Column) : data(Code) {
currentFile = File;
pos.currentLineStart = pos.tokenStart = data;
pos.currentLine = Line;
reset(pos);
}
void CScriptLex::reset(const POS &toPos) { ///< Reset this lex so we can start again
dataPos = toPos.tokenStart;
tk = last_tk = 0;
tkStr = "";
pos = toPos;
lineBreakBeforeToken = false;
currCh = nextCh = 0;
getNextCh(); // currCh
getNextCh(); // nextCh
getNextToken();
}
void CScriptLex::check(int expected_tk, int alternate_tk/*=-1*/) {
if (expected_tk==';' && tk==LEX_EOF) return; // ignore last missing ';'
if (tk!=expected_tk && tk!=al
tiny-js-42tiny-js_javascript_presskgq_源码
版权申诉
49 浏览量
2021-10-01
02:53:56
上传
评论
收藏 117KB ZIP 举报
西西nayss
- 粉丝: 71
- 资源: 4754
最新资源
- Pytorch-pytorch深度学习教程之逻辑回归.zip
- Pytorch-pytorch深度学习教程之双向循环网络.zip
- Pytorch-pytorch深度学习教程之卷积神经网络.zip
- Pytorch-pytorch深度学习教程之前馈神经网络.zip
- Pytorch-pytorch深度学习教程之线性回归.zip
- Pytorch-pytorch深度学习教程之基本操作.zip
- 基于QT的地图可视化桌面系统后台数据库为MySQL5.7源码.zip
- 基于simulink的PLL锁相环系统仿真【包括模型,文档,参考文献,操作步骤】
- 基于EM-GMM模型的目标跟踪和异常行为检测matlab仿真【包括程序,注释,参考文献,操作步骤,说明文档】
- 2109010044_胡晨燕_选课管理数据库设计与实现.prj
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈