Skip to content
This repository
Browse code

Add console object

  • Loading branch information...
commit f6fc4a8f8c6a62512437fb03eb6e8796dc48870a 1 parent d3efae1
Yusuke Suzuki authored
16 iv/lv5/context.h
@@ -57,24 +57,26 @@ class Context : public radio::HeapObject<radio::POINTER_CLEANUP> {
57 57
58 58 void Initialize();
59 59
  60 + void Import(const core::StringPiece& name, JSObject* obj) {
  61 + Error::Dummy e;
  62 + const Symbol sym = Intern(name);
  63 + global_env_->CreateMutableBinding(this, sym, false, IV_LV5_ERROR_VOID(&e));
  64 + global_env_->SetMutableBinding(this, sym, obj, false, &e);
  65 + }
  66 +
60 67 template<typename Func>
61 68 void DefineFunction(const Func& f,
62 69 const core::StringPiece& func_name,
63 70 std::size_t n) {
64   - Error::Dummy e;
65 71 JSFunction* const func = JSNativeFunction::New(this, f, n);
66   - const Symbol name = Intern(func_name);
67   - global_env_->CreateMutableBinding(this, name, false, IV_LV5_ERROR_VOID(&e));
68   - global_env_->SetMutableBinding(this, name, func, false, &e);
  72 + Import(func_name, func);
69 73 }
70 74
71 75 template<JSVal (*func)(const Arguments&, Error*), std::size_t n>
72 76 void DefineFunction(const core::StringPiece& func_name) {
73   - Error::Dummy e;
74 77 const Symbol name = Intern(func_name);
75 78 JSFunction* const f = JSInlinedFunction<func, n>::New(this, name);
76   - global_env_->CreateMutableBinding(this, name, false, IV_LV5_ERROR_VOID(&e));
77   - global_env_->SetMutableBinding(this, name, f, false, &e);
  79 + Import(func_name, f);
78 80 }
79 81
80 82 JSFunction* throw_type_error() {
1  iv/lv5/lv5.h
@@ -17,6 +17,7 @@
17 17 #include <iv/lv5/program.h>
18 18 #include <iv/lv5/railgun/railgun.h>
19 19 #include <iv/lv5/teleporter/teleporter.h>
  20 +#include <iv/lv5/breaker/breaker.h>
20 21 #include <iv/lv5/radio/radio.h>
21 22 namespace iv {
22 23 namespace lv5 {
47 iv/lv5/main.cc
@@ -11,26 +11,31 @@
11 11 #include <iv/lv5/railgun/command.h>
12 12 #include <iv/lv5/railgun/interactive.h>
13 13 #include <iv/lv5/teleporter/interactive.h>
14   -#include <iv/lv5/breaker/breaker.h>
  14 +#include <iv/lv5/melt/melt.h>
15 15 #if defined(IV_OS_MACOSX) || defined(IV_OS_LINUX) || defined(IV_OS_BSD)
16 16 #include <signal.h>
17 17 #endif
18 18
19 19 namespace {
20 20
  21 +void InitContext(iv::lv5::Context* ctx) {
  22 + iv::lv5::Error::Dummy dummy;
  23 + ctx->DefineFunction<&iv::lv5::Print, 1>("print");
  24 + ctx->DefineFunction<&iv::lv5::Log, 1>("log"); // this is simply output log function
  25 + ctx->DefineFunction<&iv::lv5::Quit, 1>("quit");
  26 + ctx->DefineFunction<&iv::lv5::CollectGarbage, 0>("gc");
  27 + ctx->DefineFunction<&iv::lv5::HiResTime, 0>("HiResTime");
  28 + ctx->DefineFunction<&iv::lv5::railgun::Dis, 1>("dis");
  29 + ctx->DefineFunction<&iv::lv5::breaker::Run, 0>("run");
  30 + iv::lv5::melt::Console::Export(ctx, &dummy);
  31 +}
  32 +
21 33 #if defined(IV_ENABLE_JIT)
22 34 int BreakerExecute(const iv::core::StringPiece& data,
23 35 const std::string& filename, bool statistics) {
24 36 iv::lv5::Error::Standard e;
25 37 iv::lv5::breaker::Context ctx;
26   - ctx.DefineFunction<&iv::lv5::Print, 1>("print");
27   - ctx.DefineFunction<&iv::lv5::Log, 1>("log"); // this is simply output log function
28   - ctx.DefineFunction<&iv::lv5::Quit, 1>("quit");
29   - ctx.DefineFunction<&iv::lv5::CollectGarbage, 0>("gc");
30   - ctx.DefineFunction<&iv::lv5::HiResTime, 0>("HiResTime");
31   - ctx.DefineFunction<&iv::lv5::railgun::Dis, 1>("dis");
32   - ctx.DefineFunction<&iv::lv5::breaker::Run, 0>("run");
33   -
  38 + InitContext(&ctx);
34 39 std::shared_ptr<iv::core::FileSource>
35 40 src(new iv::core::FileSource(data, filename));
36 41 iv::lv5::breaker::ExecuteInGlobal(&ctx, src, &e);
@@ -45,13 +50,7 @@ int BreakerExecute(const iv::core::StringPiece& data,
45 50 int BreakerExecuteFiles(const std::vector<std::string>& filenames) {
46 51 iv::lv5::Error::Standard e;
47 52 iv::lv5::breaker::Context ctx;
48   - ctx.DefineFunction<&iv::lv5::Print, 1>("print");
49   - ctx.DefineFunction<&iv::lv5::Log, 1>("log");
50   - ctx.DefineFunction<&iv::lv5::Quit, 1>("quit");
51   - ctx.DefineFunction<&iv::lv5::CollectGarbage, 0>("gc");
52   - ctx.DefineFunction<&iv::lv5::HiResTime, 0>("HiResTime");
53   - ctx.DefineFunction<&iv::lv5::railgun::Dis, 1>("dis");
54   - ctx.DefineFunction<&iv::lv5::breaker::Run, 0>("run");
  53 + InitContext(&ctx);
55 54
56 55 std::vector<char> res;
57 56 for (std::vector<std::string>::const_iterator it = filenames.begin(),
@@ -78,13 +77,7 @@ int RailgunExecute(const iv::core::StringPiece& data,
78 77 const std::string& filename, bool statistics) {
79 78 iv::lv5::Error::Standard e;
80 79 iv::lv5::railgun::Context ctx;
81   - ctx.DefineFunction<&iv::lv5::Print, 1>("print");
82   - ctx.DefineFunction<&iv::lv5::Quit, 1>("quit");
83   - ctx.DefineFunction<&iv::lv5::CollectGarbage, 0>("gc");
84   - ctx.DefineFunction<&iv::lv5::HiResTime, 0>("HiResTime");
85   - ctx.DefineFunction<&iv::lv5::railgun::Dis, 1>("dis");
86   - ctx.DefineFunction<&iv::lv5::railgun::Run, 0>("run");
87   - ctx.DefineFunction<&iv::lv5::railgun::StackDepth, 0>("StackDepth");
  80 + InitContext(&ctx);
88 81
89 82 std::shared_ptr<iv::core::FileSource>
90 83 src(new iv::core::FileSource(data, filename));
@@ -103,13 +96,7 @@ int RailgunExecute(const iv::core::StringPiece& data,
103 96 int RailgunExecuteFiles(const std::vector<std::string>& filenames) {
104 97 iv::lv5::Error::Standard e;
105 98 iv::lv5::railgun::Context ctx;
106   - ctx.DefineFunction<&iv::lv5::Print, 1>("print");
107   - ctx.DefineFunction<&iv::lv5::Quit, 1>("quit");
108   - ctx.DefineFunction<&iv::lv5::CollectGarbage, 0>("gc");
109   - ctx.DefineFunction<&iv::lv5::HiResTime, 0>("HiResTime");
110   - ctx.DefineFunction<&iv::lv5::railgun::Dis, 1>("dis");
111   - ctx.DefineFunction<&iv::lv5::railgun::Run, 0>("run");
112   - ctx.DefineFunction<&iv::lv5::railgun::StackDepth, 0>("StackDepth");
  99 + InitContext(&ctx);
113 100
114 101 std::vector<char> res;
115 102 for (std::vector<std::string>::const_iterator it = filenames.begin(),
10 iv/lv5/melt/README.md
Source Rendered
... ... @@ -0,0 +1,10 @@
  1 +# iv / lv5 / melt
  2 +
  3 +melt (Meltdowner) is namespace for ECMAScript extensions.
  4 +
  5 +## Modules
  6 +
  7 +* Console
  8 + * use this [draft](http://sideshowbarker.github.com/console-spec/)
  9 +
  10 +
42 iv/lv5/melt/console/console.h
... ... @@ -0,0 +1,42 @@
  1 +#ifndef IV_LV5_MELT_MELT_CONSOLE_CONSOLE_H_
  2 +#define IV_LV5_MELT_MELT_CONSOLE_CONSOLE_H_
  3 +#include <cstdio>
  4 +#include <iv/lv5/lv5.h>
  5 +namespace iv {
  6 +namespace lv5 {
  7 +namespace melt {
  8 +
  9 +class Console {
  10 + public:
  11 + static JSVal Log(const Arguments& args, Error* e) {
  12 + if (!args.empty()) {
  13 + Context* const ctx = args.ctx();
  14 + for (Arguments::const_iterator it = args.begin(),
  15 + last = args.end(); it != last;) {
  16 + JSString* const str = it->ToString(ctx, IV_LV5_ERROR(e));
  17 + std::cout << *str;
  18 + if (++it != last) {
  19 + std::fputc(' ', stdout);
  20 + }
  21 + }
  22 + std::fputc('\n', stdout);
  23 + }
  24 + std::fflush(stdout);
  25 + return JSUndefined;
  26 + }
  27 +
  28 + static void Export(Context* ctx, Error* e) {
  29 + JSObject* obj = JSObject::New(ctx);
  30 + const Symbol name = ctx->Intern("log");
  31 + obj->DefineOwnProperty(
  32 + ctx, name,
  33 + DataDescriptor(
  34 + JSInlinedFunction<&Log, 1>::New(ctx, name),
  35 + ATTR::W | ATTR::C),
  36 + false, e);
  37 + ctx->Import("console", obj);
  38 + }
  39 +};
  40 +
  41 +} } } // namespace iv::lv5::melt
  42 +#endif // IV_LV5_MELT_MELT_CONSOLE_CONSOLE_H_
6 iv/lv5/melt/melt.h
... ... @@ -0,0 +1,6 @@
  1 +#ifndef IV_LV5_MELT_MELT_H_
  2 +#define IV_LV5_MELT_MELT_H_
  3 +
  4 +#include <iv/lv5/melt/console/console.h>
  5 +
  6 +#endif // IV_LV5_MELT_MELT_H_
3  iv/lv5/railgun/interactive.h
@@ -15,6 +15,7 @@
15 15 #include <iv/lv5/command.h>
16 16 #include <iv/lv5/railgun/railgun.h>
17 17 #include <iv/lv5/railgun/command.h>
  18 +#include <iv/lv5/melt/melt.h>
18 19 namespace iv {
19 20 namespace lv5 {
20 21 namespace railgun {
@@ -30,6 +31,7 @@ class Interactive {
30 31 explicit Interactive(bool disassemble)
31 32 : ctx_(),
32 33 disassemble_(disassemble) {
  34 + Error::Dummy dummy;
33 35 ctx_.DefineFunction<&Print, 1>("print");
34 36 ctx_.DefineFunction<&Quit, 1>("quit");
35 37 ctx_.DefineFunction<&CollectGarbage, 0>("gc");
@@ -37,6 +39,7 @@ class Interactive {
37 39 ctx_.DefineFunction<&railgun::Run, 0>("run");
38 40 ctx_.DefineFunction<&railgun::StackDepth, 0>("StackDepth");
39 41 ctx_.DefineFunction<&railgun::Dis, 1>("dis");
  42 + melt::Console::Export(&ctx_, &dummy);
40 43 }
41 44
42 45 int Run() {
3  iv/lv5/teleporter/interactive.h
@@ -14,6 +14,7 @@
14 14 #include <iv/lv5/jsstring.h>
15 15 #include <iv/lv5/command.h>
16 16 #include <iv/lv5/teleporter/teleporter.h>
  17 +#include <iv/lv5/melt/melt.h>
17 18 namespace iv {
18 19 namespace lv5 {
19 20 namespace teleporter {
@@ -28,9 +29,11 @@ class Interactive {
28 29 public:
29 30 Interactive()
30 31 : ctx_() {
  32 + Error::Dummy dummy;
31 33 ctx_.DefineFunction<&lv5::Print, 1>("print");
32 34 ctx_.DefineFunction<&lv5::Quit, 1>("quit");
33 35 ctx_.DefineFunction<&iv::lv5::HiResTime, 0>("HiResTime");
  36 + melt::Console::Export(&ctx_, &dummy);
34 37 }
35 38 int Run() {
36 39 std::vector<char> buffer;

0 comments on commit f6fc4a8

Please sign in to comment.
Something went wrong with that request. Please try again.