void tTJSInterCodeContext::_output_func(const tjs_char *msg,
	const tjs_char *comment, tjs_int addr, const tjs_int32 *codestart,
		tjs_int size, void *data)

{
	tjs_int buflen = TJS_strlen(msg) + TJS_strlen(comment) + 20;
	tjs_char *buf = new tjs_char[buflen];

	TJS_snprintf(buf, buflen, TJS_W("%08d %ls"), addr, msg);
	if(comment[0])
	{
		TJS_strcat(buf, TJS_W("\t// "));
		TJS_strcat(buf, comment);
	}

	try
	{
		of_data *dat = (of_data *)(data);
		dat->func(buf, dat->funcdata);
	}
	catch(...)
	{
		delete [] buf;
		throw;
	}

	delete [] buf;
}
Exemple #2
0
/*
	these functions do :
	replace each %%, %1, %2 into %, p1, p2.
	%1 must appear only once in the message string, otherwise internal
	buffer will overflow. ( %2 must also so )
*/
ttstr TVPFormatMessage(const tjs_char *msg, const ttstr & p1)
{
	tjs_char *p;
	tjs_char * buf = new tjs_char[TJS_strlen(msg) + p1.GetLen() + 1];
	p = buf;
	for(;*msg;msg++,p++)
	{
		if(*msg == TJS_W('%'))
		{
			if(msg[1] == TJS_W('%'))
			{
				// %%
				*p = TJS_W('%');
				msg++;
				continue;
			}
			else if(msg[1] == TJS_W('1'))
			{
				// %1
				TJS_strcpy(p, p1.c_str());
				p += p1.GetLen();
				p--;
				msg++;
				continue;
			}
		}
		*p = *msg;
	}

	*p = 0;

	ttstr ret(buf);
	delete [] buf;
	return ret;
}
Exemple #3
0
//---------------------------------------------------------------------------
void tTJS::OutputToConsoleWithCentering(const tjs_char *msg, tjs_uint width) const
{
	// this function does not matter whether msg includes ZENKAKU characters ...
	if(!msg) return;
	tjs_int len = TJS_strlen(msg);
	tjs_int ns = ((tjs_int)width - len)/2;
	if(ns<=0)
	{
		OutputToConsole(msg);
	}
	else
	{
		tjs_char *outbuf = new tjs_char[ns + len +1];
		tjs_char *p = outbuf;
		while(ns--) *(p++)= TJS_W(' ');
		TJS_strcpy(p, msg);
		try
		{
			OutputToConsole(outbuf);
		}
		catch(...)
		{
			delete [] outbuf;
			throw;
		}

		delete [] outbuf;
	}
}
//---------------------------------------------------------------------------
void tTJSScriptBlock::SetName(const tjs_char *name, tjs_int lineofs)
{
    if(Name) delete [] Name, Name = NULL;
    if(name)
    {
        LineOffset = lineofs;
        Name = new tjs_char[ TJS_strlen(name) + 1];
        TJS_strcpy(Name, name);
    }
}
void tTJSInterCodeContext::_output_func_src(const tjs_char *msg,
	const tjs_char *name, tjs_int line, void *data)
{
	tjs_int buflen = TJS_strlen(msg) + TJS_strlen(name) + 20;
	tjs_char *buf = new tjs_char[buflen];
	if(line >= 0)
		TJS_snprintf(buf, buflen, TJS_W("#%ls(%d) %ls"), name, line+1, msg);
	else
		TJS_snprintf(buf, buflen, TJS_W("#%ls %ls"), name, msg);
	try
	{
		of_data *dat = (of_data *)(data);
		dat->func(buf, dat->funcdata);
	}
	catch(...)
	{
		delete [] buf;
		throw;
	}

	delete [] buf;
}
//---------------------------------------------------------------------------
size_t TJS_wcstombs(tjs_nchar *s, const tjs_char *pwcs, size_t n)
{
	if(s && !n) return 0;

	BOOL useddefault = FALSE;
	if(s)
	{
		// Try converting to multibyte. Here assumes s is large enough to
		// store the result.
		size_t pwcs_len = TJS_strlen(pwcs);
		size_t count = WideCharToMultiByte(CP_ACP, 0,
			pwcs, pwcs_len, s, n, NULL, &useddefault);

		if(count != 0/* && !useddefault*/)
			return count;

		if(/*useddefault || */GetLastError () != ERROR_INSUFFICIENT_BUFFER)
			return (size_t) -1; // may a conversion error

		// Buffer is not enough to store the result ...
		while(count < n)
		{
			char buffer[TJS_MB_MAX_CHARLEN + 1];
			int retval = WideCharToMultiByte(CP_ACP, 0, pwcs, 1, buffer,
				TJS_MB_MAX_CHARLEN, NULL, &useddefault);
			if(retval == 0/* || useddefault*/)
				return (size_t) -1;

			if(count + retval > n)
				return count;

			for(int i = 0; i < retval; i++, count++)
			{
				if((s[count] = buffer[i]) == '\0') return count;
			}

			pwcs ++;
		}

		return count;
	}
	else
	{
		// Returns the buffer size to store the result
		int count = WideCharToMultiByte(CP_ACP, 0,
			pwcs, -1, NULL, 0, NULL, &useddefault);
		if(count == 0/* || useddefault*/) return (size_t) -1;
		return count - 1;
	}
}
Exemple #7
0
//---------------------------------------------------------------------------
void tTJS::OutputToConsoleSeparator(const tjs_char *text, tjs_uint count) const
{
	tjs_int len = TJS_strlen(text);
	tjs_char *outbuf = new tjs_char [ len * count + 1];
	tjs_char *p = outbuf;
	while(count--)
	{
		TJS_strcpy(p, text);
		p += len;
	}

	try
	{
		OutputToConsole(outbuf);
	}
	catch(...)
	{
		delete [] outbuf;
		throw;
	}

	delete [] outbuf;
}
void tTJSScriptBlock::SetText(tTJSVariant *result, const tjs_char *text,
                              iTJSDispatch2 * context, bool isexpression)
{
    TJS_F_TRACE("tTJSScriptBlock::SetText");


    // compiles text and executes its global level scripts.
    // the script will be compiled as an expression if isexpressn is true.
    if(!text) return;
    if(!text[0]) return;

    TJS_D((TJS_W("Counting lines ...\n")))

    Script = new tjs_char[TJS_strlen(text)+1];
    TJS_strcpy(Script, text);

    // calculation of line-count
    tjs_char *ls = Script;
    tjs_char *p = Script;
    while(*p)
    {
        if(*p == TJS_W('\r') || *p == TJS_W('\n'))
        {
            LineVector.push_back(int(ls - Script));
            LineLengthVector.push_back(int(p - ls));
            if(*p == TJS_W('\r') && p[1] == TJS_W('\n')) p++;
            p++;
            ls = p;
        }
        else
        {
            p++;
        }
    }

    if(p!=ls)
    {
        LineVector.push_back(int(ls - Script));
        LineLengthVector.push_back(int(p - ls));
    }

    try
    {

        // parse and execute
#ifdef TJS_DEBUG_PROFILE_TIME
        {
            tTJSTimeProfiler p(parsetime);
#endif

            Parse(text, isexpression, result != NULL);

#ifdef TJS_DEBUG_PROFILE_TIME
        }

        {
            char buf[256];
            sprintf(buf, "parsing : %d", parsetime);
            OutputDebugString(buf);
            if(parsetime)
            {
                sprintf(buf, "Commit : %d (%d%%)", time_Commit, time_Commit*100/parsetime);
                OutputDebugString(buf);
                sprintf(buf, "yylex : %d (%d%%)", time_yylex, time_yylex*100/parsetime);
                OutputDebugString(buf);
                sprintf(buf, "MakeNP : %d (%d%%)", time_make_np, time_make_np*100/parsetime);
                OutputDebugString(buf);
                sprintf(buf, "GenNodeCode : %d (%d%%)", time_GenNodeCode, time_GenNodeCode*100/parsetime);
                OutputDebugString(buf);
                sprintf(buf, "  PutCode : %d (%d%%)", time_PutCode, time_PutCode*100/parsetime);
                OutputDebugString(buf);
                sprintf(buf, "  PutData : %d (%d%%)", time_PutData, time_PutData*100/parsetime);
                OutputDebugString(buf);
                sprintf(buf, "  this_proxy : %d (%d%%)", time_this_proxy, time_this_proxy*100/parsetime);
                OutputDebugString(buf);

                sprintf(buf, "ns::Push : %d (%d%%)", time_ns_Push, time_ns_Push*100/parsetime);
                OutputDebugString(buf);
                sprintf(buf, "ns::Pop : %d (%d%%)", time_ns_Pop, time_ns_Pop*100/parsetime);
                OutputDebugString(buf);
                sprintf(buf, "ns::Find : %d (%d%%)", time_ns_Find, time_ns_Find*100/parsetime);
                OutputDebugString(buf);
                sprintf(buf, "ns::Remove : %d (%d%%)", time_ns_Remove, time_ns_Remove*100/parsetime);
                OutputDebugString(buf);
                sprintf(buf, "ns::Commit : %d (%d%%)", time_ns_Commit, time_ns_Commit*100/parsetime);
                OutputDebugString(buf);

            }
        }
#endif

#ifdef TJS_DEBUG_DISASM
        std::list<tTJSInterCodeContext *>::iterator i =
            InterCodeContextList.begin();
        while(i != InterCodeContextList.end())
        {
            ConsoleOutput(TJS_W(""), (void*)this);
            ConsoleOutput((*i)->GetName(), (void*)this);
            (*i)->Disassemble(ConsoleOutput, (void*)this);
            i++;
        }
#endif

        // execute global level script
        ExecuteTopLevelScript(result, context);
    }
    catch(...)
    {
        if(InterCodeContextList.size() != 1)
        {
            if(TopLevelContext) TopLevelContext->Release(), TopLevelContext = NULL;
            while(ContextStack.size())
            {
                ContextStack.top()->Release();
                ContextStack.pop();
            }
        }
        throw;
    }

    if(InterCodeContextList.size() != 1)
    {
        // this is not a single-context script block
        // (may hook itself)
        // release all contexts and global at this time
        if(TopLevelContext) TopLevelContext->Release(), TopLevelContext = NULL;
        while(ContextStack.size())
        {
            ContextStack.top()->Release();
            ContextStack.pop();
        }
    }
}