Ignore:
Timestamp:
03/04/13 20:13:19 (12 years ago)
Author:
tbretz
Message:
Implemented to possibility to run an interactive javascript interpreter.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/FACT++/src/InterpreterV8.cc

    r14741 r14983  
    2323#include "dim.h"
    2424#include "tools.h"
     25#include "Readline.h"
    2526#include "externals/izstream.h"
     27
     28#include "WindowLog.h"
    2629
    2730using namespace std;
     
    17191722        // Print (filename):(line number): (message).
    17201723        const String::AsciiValue filename(message->GetScriptResourceName());
    1721 
    1722         out << *filename;
    1723         if (message->GetLineNumber()>0)
    1724             out << ": l." << message->GetLineNumber();
    1725         if (*exception)
    1726             out << ": ";
     1724        if (filename.length()>0)
     1725        {
     1726            out << *filename;
     1727            if (message->GetLineNumber()>0)
     1728                out << ": l." << message->GetLineNumber();
     1729            if (*exception)
     1730                out << ": ";
     1731        }
    17271732    }
    17281733
     
    18431848
    18441849    return !ExecuteCode(buffer, name, main).IsEmpty();
     1850}
     1851
     1852bool InterpreterV8::ExecuteConsole()
     1853{
     1854    JsSetState(3);
     1855
     1856    WindowLog lout;
     1857    lout << "\n " << kUnderline << " JavaScript interpreter " << kReset << " (enter '.q' to quit)\n" << endl;
     1858
     1859    Readline::StaticPushHistory("java.his");
     1860
     1861    string command;
     1862
     1863    while (1)
     1864    {
     1865        const string buffer = Tools::Trim(Readline::StaticPrompt(command.empty() ? "JS> " : " \\> "));
     1866        if (buffer==".q")
     1867            break;
     1868
     1869        // buffer empty, do nothing
     1870        if (buffer.empty())
     1871            continue;
     1872
     1873        // Compose command
     1874        if (!command.empty())
     1875            command += ' ';
     1876        command += buffer;
     1877
     1878        // If line ends with a backslash, allow addition of next line
     1879        if (command.back()=='\\')
     1880        {
     1881            command[command.length()-1] = ' ';
     1882            command = Tools::Trim(command);
     1883            continue;
     1884        }
     1885
     1886        // Catch exceptions during code compilation
     1887        TryCatch exception;
     1888
     1889        // Execute code which was entered
     1890        bool rc = ExecuteCode(command, "", false).IsEmpty();
     1891        if (exception.HasCaught())
     1892        {
     1893            HandleException(exception, "compile");
     1894            rc = true;
     1895        }
     1896
     1897        // In case of an exception add an empty line to the output
     1898        if (rc)
     1899            lout << endl;
     1900
     1901        // command has been executed, collect new command
     1902        command = "";
     1903    }
     1904
     1905    lout << endl;
     1906
     1907    Readline::StaticPopHistory("java.his");
     1908
     1909    return true;
    18451910}
    18461911
     
    20722137        Locker::StartPreemption(10);
    20732138
    2074         rc &= ExecuteFile(filename, true);
     2139        rc &= filename.empty() ? ExecuteConsole() : ExecuteFile(filename, true);
    20752140
    20762141        Locker::StopPreemption();
Note: See TracChangeset for help on using the changeset viewer.