Skip to content

Commit

Permalink
option, feat: support -e on command line.
Browse files Browse the repository at this point in the history
  • Loading branch information
xicilion committed Jan 22, 2023
1 parent e9efa07 commit ebbbc90
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 60 deletions.
3 changes: 2 additions & 1 deletion fibjs/include/Isolate.h
Expand Up @@ -50,7 +50,7 @@ class Isolate : public exlib::linkitem {
};

public:
Isolate(exlib::string jsFilename);
Isolate(exlib::string jsFilename, exlib::string jsCode = "");

public:
static Isolate* current();
Expand Down Expand Up @@ -131,6 +131,7 @@ class Isolate : public exlib::linkitem {
int32_t m_id;
int32_t m_hr;
exlib::string m_fname;
exlib::string m_jsCode;

QuickArray<void*> m_classInfo;

Expand Down
4 changes: 2 additions & 2 deletions fibjs/include/SandBox.h
Expand Up @@ -75,7 +75,7 @@ class SandBox : public SandBox_base {
class Context {
public:
Context(SandBox* sb, exlib::string id);
static result_t repl();
static result_t repl(exlib::string src);

public:
obj_ptr<SandBox> m_sb;
Expand Down Expand Up @@ -170,7 +170,7 @@ class SandBox : public SandBox_base {
result_t resolve(exlib::string base, exlib::string& id, obj_ptr<Buffer_base>& data,
v8::Local<v8::Value>& retVal);

result_t repl();
result_t repl(exlib::string src);

result_t run_module(exlib::string id, exlib::string base, v8::Local<v8::Value>& retVal);
result_t run_main(exlib::string fname, v8::Local<v8::Array> argv);
Expand Down
2 changes: 2 additions & 0 deletions fibjs/include/options.h
Expand Up @@ -23,6 +23,8 @@ extern FILE* g_cov;
extern bool g_tcpdump;
extern bool g_ssldump;

extern exlib::string g_exec_code;

extern bool g_uv_socket;

struct OptData {
Expand Down
7 changes: 5 additions & 2 deletions fibjs/src/base/Isolate.cpp
Expand Up @@ -107,7 +107,7 @@ class ShellArrayBufferAllocator : public v8::ArrayBuffer::Allocator {
}
};

Isolate::Isolate(exlib::string jsFilename)
Isolate::Isolate(exlib::string jsFilename, exlib::string jsCode)
: m_id((int32_t)s_iso_id.inc())
, m_hr(0)
, m_ipc_mode(0)
Expand All @@ -129,7 +129,10 @@ Isolate::Isolate(exlib::string jsFilename)
{
s_isolates.putTail(this);

m_fname = jsFilename;
if (jsCode.empty())
m_fname = jsFilename;
else
m_jsCode = jsCode;

static v8::Isolate::CreateParams create_params;
static ShellArrayBufferAllocator array_buffer_allocator;
Expand Down
2 changes: 1 addition & 1 deletion fibjs/src/base/Runtime.cpp
Expand Up @@ -76,7 +76,7 @@ void start(int32_t argc, char** argv, result_t (*jsEntryFiber)(Isolate*), Isolat
static void FirstFiber(void* p)
{
EntryThread* th = (EntryThread*)p;
Isolate* isolate = new Isolate(th->m_fibjsEntry);
Isolate* isolate = new Isolate(th->m_fibjsEntry, g_exec_code);
syncCall(isolate, th->m_jsFiber, isolate);
}

Expand Down
2 changes: 1 addition & 1 deletion fibjs/src/base/fibjs_main.cpp
Expand Up @@ -53,7 +53,7 @@ result_t FiberProcJsEntry(Isolate* isolate)
pModule = pModule->m_next;
}

s.m_hr = isolate->m_topSandbox->repl();
s.m_hr = isolate->m_topSandbox->repl(isolate->m_jsCode);
}

return s.m_hr;
Expand Down
43 changes: 26 additions & 17 deletions fibjs/src/base/options.cpp
Expand Up @@ -31,6 +31,8 @@ bool g_ssldump = false;

bool g_uv_socket = false;

exlib::string g_exec_code;

#ifdef DEBUG
#define GUARD_SIZE 32
#else
Expand Down Expand Up @@ -63,6 +65,8 @@ static void printHelp()
" -h, --help print fibjs command line options.\n"
" -v, --version print fibjs version.\n"
"\n"
" -e code evaluate script\n"
"\n"
" --use-thread run fibjs in thread mode.\n"
" --tcpdump print out the contents of the tcp package.\n"
" --ssldump print out the contents of the ssl package.\n"
Expand All @@ -89,30 +93,29 @@ static void printHelp()

void options(int32_t& pos, char* argv[])
{
int32_t argc = pos;
int32_t i;
int32_t df = 0;

for (i = 1; i < pos; i++) {
char* arg = argv[i];

if (df)
argv[i - df] = arg;

for (pos = 1; (pos < argc) && (argv[pos][0] == '-'); pos++)
if (argv[pos][1] == '-') {
if (arg[0] != '-')
break;
else if (arg[1] == '-') {
int32_t j;
exlib::string tmp("opt_tools/");
tmp += argv[pos] + 2;
tmp += arg + 2;

for (i = 0; opt_tools[i].name && qstrcmp(opt_tools[i].name, tmp.c_str()); i++)
for (j = 0; opt_tools[j].name && qstrcmp(opt_tools[j].name, tmp.c_str()); j++)
;

if (opt_tools[i].name)
if (opt_tools[j].name)
break;
}

argc = pos;
int32_t df = 0;

for (int32_t i = 0; i < argc; i++) {
char* arg = argv[i];

if (df)
argv[i - df] = arg;

if (!qstrcmp(arg, "--help") || !qstrcmp(arg, "-h")) {
printHelp();
_exit(0);
Expand Down Expand Up @@ -157,14 +160,20 @@ void options(int32_t& pos, char* argv[])
_exit(0);
}
df++;
} else if (!qstrcmp(arg, "-e")) {
if (i + 1 < pos) {
g_exec_code = argv[i + 1];
i++;
df += 2;
}
} else if (!qstrcmp(arg, "--v8-options")) {
v8::internal::FlagList::PrintHelp();
_exit(0);
}
}

if (df)
argc -= df;
pos = i;
int32_t argc = pos - df;

v8::V8::SetFlagsFromCommandLine(&argc, argv, true);
}
Expand Down
76 changes: 40 additions & 36 deletions fibjs/src/sandbox/SandBox_repl.cpp
Expand Up @@ -30,36 +30,64 @@ void output(int32_t priority, exlib::string msg)
asyncLog(console_base::C_PRINT, msg);
}

result_t SandBox::repl()
result_t SandBox::repl(exlib::string src)
{
Context context(this, "repl");
return Context::repl();
return Context::repl(src);
}

extern std_logger* s_std;
exlib::string appname("fibjs");

result_t SandBox::Context::repl()
result_t SandBox::Context::repl(exlib::string src)
{
result_t hr = 0;
exlib::string buf;
v8::Local<v8::Value> v, v1;
exlib::string buf(src);
v8::Local<v8::Value> v;
Isolate* isolate = Isolate::current();
v8::Local<v8::String> strFname = isolate->NewString("repl", 4);
v8::Local<v8::String> strFname;
obj_ptr<BufferedStream_base> bs;

exlib::string str_ver("Welcome to " + appname + " ");
if (src.empty()) {
strFname = isolate->NewString("[repl]", 6);

str_ver += fibjs_version;
str_ver += '.';
output(console_base::C_INFO, str_ver);
output(console_base::C_INFO, "Type \".help\" for more information.");
exlib::string str_ver("Welcome to " + appname + " ");

str_ver += fibjs_version;
str_ver += '.';
output(console_base::C_INFO, str_ver);
output(console_base::C_INFO, "Type \".help\" for more information.");
} else
strFname = isolate->NewString("[eval]", 6);

while (true) {
if (!buf.empty()) {
TryCatch try_catch;

v8::ScriptOrigin origin(isolate->m_isolate, strFname);
v8::Local<v8::Context> context = isolate->m_isolate->GetCurrentContext();
v8::Local<v8::Script> script = v8::Script::Compile(context, isolate->NewString(buf), &origin).FromMaybe(v8::Local<v8::Script>());

if (script.IsEmpty()) {
if (isolate->toString(try_catch.Exception()) != "SyntaxError: Unexpected end of input" || !src.empty()) {
buf.clear();
ReportException(try_catch, 0, true);
}
} else {
buf.clear();

v = script->Run(context).FromMaybe(v8::Local<v8::Value>());
if (v.IsEmpty())
ReportException(try_catch, 0, true);
}
}

if (!v.IsEmpty() && !v->IsUndefined())
console_base::dir(v, v8::Local<v8::Object>());
v.Clear();

v = v1;
if (!src.empty())
break;

exlib::string line;
hr = console_base::ac_readLine(buf.empty() ? "> " : " ... ", line);
Expand Down Expand Up @@ -99,30 +127,6 @@ result_t SandBox::Context::repl()

buf += line;
buf.append("\n", 1);

{
TryCatch try_catch;

v8::ScriptOrigin origin(isolate->m_isolate, strFname);
v8::Local<v8::Context> context = isolate->m_isolate->GetCurrentContext();
v8::Local<v8::Script> script = v8::Script::Compile(context, isolate->NewString(buf), &origin).FromMaybe(v8::Local<v8::Script>());

if (script.IsEmpty()) {
if (isolate->toString(try_catch.Exception()) != "SyntaxError: Unexpected end of input") {
buf.clear();
ReportException(try_catch, 0, true);
}
continue;
}

buf.clear();

v = script->Run(context).FromMaybe(v8::Local<v8::Value>());
if (v.IsEmpty()) {
ReportException(try_catch, 0, true);
continue;
}
}
}

return hr;
Expand Down

0 comments on commit ebbbc90

Please sign in to comment.