more weird js syntax handling

This commit is contained in:
Eliot Horowitz
2009-05-21 10:05:26 -04:00
parent 0ca5213f8b
commit ed74bbf25a
2 changed files with 23 additions and 2 deletions

View File

@@ -208,11 +208,29 @@ namespace mongo {
return code[8] == ' ' || code[8] == '(';
}
bool isSimpleStatement( const string& code ){
if ( code.find( "return" ) != string::npos )
return false;
if ( code.find( ";" ) != string::npos &&
code.find( ";" ) != code.rfind( ";" ) )
return false;
if ( code.find( "for(" ) != string::npos ||
code.find( "for (" ) != string::npos ||
code.find( "while (" ) != string::npos ||
code.find( "while(" ) != string::npos )
return false;
return true;
}
JSFunction * compileFunction( const char * code ){
if ( ! hasFunctionIdentifier( code ) ){
string s = code;
if ( strstr( code , "return" ) == 0 )
if ( isSimpleStatement( s ) ){
s = "return " + s;
}
return JS_CompileFunction( _context , 0 , "anonymous" , 0 , 0 , s.c_str() , strlen( s.c_str() ) , "nofile_a" , 0 );
}