A simple shell that takes a list of expressions on the command-line and executes them.
#include <assert.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
bool report_exceptions);
static bool run_shell;
int main(int argc, char* argv[]) {
run_shell = (argc == 1);
int result;
{
fprintf(stderr, "Error creating context\n");
return 1;
}
result = RunMain(isolate, context, platform.get(), argc, argv);
if (run_shell) RunShell(isolate, context, platform.get());
}
return result;
}
return *value ? *value : "<string conversion failed>";
}
}
bool first = true;
for (
int i = 0; i < info.
Length(); i++) {
if (first) {
first = false;
} else {
printf(" ");
}
const char* cstr = ToCString(str);
printf("%s", cstr);
}
printf("\n");
fflush(stdout);
}
return;
}
if (*file == nullptr) {
return;
}
if (!ReadFile(info.
GetIsolate(), *file).ToLocal(&source)) {
return;
}
}
for (
int i = 0; i < info.
Length(); i++) {
if (*file == nullptr) {
return;
}
if (!ReadFile(info.
GetIsolate(), *file).ToLocal(&source)) {
return;
}
if (!ExecuteString(info.
GetIsolate(), source, info[i],
false,
false)) {
return;
}
}
}
int exit_code =
fflush(stdout);
fflush(stderr);
exit(exit_code);
}
.ToLocalChecked());
}
FILE* file = fopen(name, "rb");
if (file == nullptr) return {};
fseek(file, 0, SEEK_END);
size_t size = ftell(file);
rewind(file);
char* chars = new char[size + 1];
chars[size] = '\0';
for (size_t i = 0; i < size;) {
i += fread(&chars[i], 1, size - i, file);
if (ferror(file)) {
fclose(file);
return {};
}
}
fclose(file);
delete[] chars;
return result;
}
for (int i = 1; i < argc; i++) {
const char* str = argv[i];
if (strcmp(str, "--shell") == 0) {
run_shell = true;
} else if (strcmp(str, "-f") == 0) {
continue;
} else if (strncmp(str, "--", 2) == 0) {
fprintf(stderr,
"Warning: unknown flag %s.\nTry --help for options\n", str);
} else if (strcmp(str, "-e") == 0 && i + 1 < argc) {
bool success;
{
return 1;
}
success = ExecuteString(isolate, source, file_name, false, true);
}
if (!success) return 1;
} else {
bool success;
{
if (!ReadFile(isolate, str).ToLocal(&source)) {
fprintf(stderr, "Error reading '%s'\n", str);
continue;
}
success = ExecuteString(isolate, source, file_name, false, true);
}
if (!success) return 1;
}
}
return 0;
}
static const int kBufferSize = 256;
while (true) {
char buffer[kBufferSize];
fprintf(stderr, "> ");
char* str = fgets(buffer, kBufferSize, stdin);
if (str == nullptr) break;
{
ExecuteString(isolate,
name, true, true);
}
}
fprintf(stderr, "\n");
}
bool report_exceptions) {
if (report_exceptions)
ReportException(isolate, &try_catch);
return false;
} else {
if (!script->Run(context).ToLocal(&result)) {
assert(try_catch.HasCaught());
if (report_exceptions)
ReportException(isolate, &try_catch);
return false;
} else {
assert(!try_catch.HasCaught());
if (print_result && !result->IsUndefined()) {
const char* cstr = ToCString(str);
printf("%s\n", cstr);
}
return true;
}
}
}
const char* exception_string = ToCString(exception);
fprintf(stderr, "%s\n", exception_string);
} else {
message->GetScriptOrigin().ResourceName());
const char* filename_string = ToCString(filename);
int linenum = message->GetLineNumber(context).FromJust();
fprintf(stderr, "%s:%i: %s\n", filename_string, linenum, exception_string);
isolate, message->GetSourceLine(context).ToLocalChecked());
const char* sourceline_string = ToCString(sourceline);
fprintf(stderr, "%s\n", sourceline_string);
int start = message->GetStartColumn(context).FromJust();
for (int i = 0; i < start; i++) {
fprintf(stderr, " ");
}
int end = message->GetEndColumn(context).FromJust();
for (int i = start; i < end; i++) {
fprintf(stderr, "^");
}
fprintf(stderr, "\n");
if (try_catch->
StackTrace(context).ToLocal(&stack_trace_string) &&
stack_trace_string->IsString() &&
const char* err = ToCString(stack_trace);
fprintf(stderr, "%s\n", err);
}
}
}
static Allocator * NewDefaultAllocator()
Definition: v8-context.h:383
static Local< Context > New(Isolate *isolate, ExtensionConfiguration *extensions=nullptr, MaybeLocal< ObjectTemplate > global_template=MaybeLocal< ObjectTemplate >(), MaybeLocal< Value > global_object=MaybeLocal< Value >(), DeserializeInternalFieldsCallback internal_fields_deserializer=DeserializeInternalFieldsCallback(), MicrotaskQueue *microtask_queue=nullptr, DeserializeContextDataCallback context_data_deserializer=DeserializeContextDataCallback(), DeserializeAPIWrapperCallback api_wrapper_deserializer=DeserializeAPIWrapperCallback())
Definition: v8-function-callback.h:118
ReturnValue< T > GetReturnValue() const
Definition: v8-function-callback.h:679
Isolate * GetIsolate() const
Definition: v8-function-callback.h:674
int Length() const
Definition: v8-function-callback.h:689
static Local< FunctionTemplate > New(Isolate *isolate, FunctionCallback callback=nullptr, Local< Value > data=Local< Value >(), Local< Signature > signature=Local< Signature >(), int length=0, ConstructorBehavior behavior=ConstructorBehavior::kAllow, SideEffectType side_effect_type=SideEffectType::kHasSideEffect, const CFunction *c_function=nullptr, uint16_t instance_type=0, uint16_t allowed_receiver_instance_type_range_start=0, uint16_t allowed_receiver_instance_type_range_end=0)
Definition: v8-persistent-handle.h:347
Definition: v8-local-handle.h:97
Definition: v8-isolate.h:306
Definition: v8-isolate.h:212
static Isolate * New(const CreateParams ¶ms)
Local< Value > ThrowError(const char(&message)[N])
Definition: v8-isolate.h:977
Local< Context > GetCurrentContext()
Definition: v8-local-handle.h:266
Local< S > As() const
Definition: v8-local-handle.h:337
Definition: v8-local-handle.h:632
static Local< ObjectTemplate > New(Isolate *isolate, Local< FunctionTemplate > constructor=Local< FunctionTemplate >())
Local< T > Get(Isolate *isolate) const
Definition: v8-persistent-handle.h:111
Definition: v8-message.h:62
static MaybeLocal< Script > Compile(Local< Context > context, Local< String > source, ScriptOrigin *origin=nullptr)
Definition: v8-primitive.h:614
Definition: v8-primitive.h:124
static MaybeLocal< String > NewFromUtf8(Isolate *isolate, const char *data, NewStringType type=NewStringType::kNormal, int length=-1)
static Local< String > NewFromUtf8Literal(Isolate *isolate, const char(&literal)[N], NewStringType type=NewStringType::kNormal)
Definition: v8-primitive.h:492
Definition: v8-exception.h:136
Local< v8::Message > Message() const
static MaybeLocal< Value > StackTrace(Local< Context > context, Local< Value > exception)
Local< Value > Exception() const
static void InitializePlatform(Platform *platform)
static const char * GetVersion()
static bool InitializeICUDefaultLocation(const char *exec_path, const char *icu_data_file=nullptr)
static bool Initialize()
Definition: v8-initialization.h:108
static void SetFlagsFromCommandLine(int *argc, char **argv, bool remove_flags)
static void DisposePlatform()
static void InitializeExternalStartupData(const char *directory_path)
bool IsEmpty() const
Definition: v8-handle-base.h:56
Definition: v8-isolate.h:217
ArrayBuffer::Allocator * array_buffer_allocator
Definition: v8-isolate.h:264