Merge pull request #1065 from monaka/pr-reduce-stdio-dependency

Reduce stdio dependency
This commit is contained in:
Yukihiro "Matz" Matsumoto
2013-03-25 07:36:47 -07:00
5 changed files with 35 additions and 10 deletions
+1
View File
@@ -8,6 +8,7 @@
#define MRUBYCONF_H
#include <stdint.h>
#include <stddef.h>
/* configuration options: */
/* add -DMRB_USE_FLOAT to use float instead of double for floating point numbers */
+8
View File
@@ -101,7 +101,9 @@ struct mrb_parser_state {
struct mrb_pool *pool;
mrb_ast_node *cells;
const char *s, *send;
#ifdef ENABLE_STDIO
FILE *f;
#endif
char *filename;
int lineno;
int column;
@@ -143,16 +145,22 @@ void mrb_parser_free(struct mrb_parser_state*);
void mrb_parser_parse(struct mrb_parser_state*,mrbc_context*);
/* utility functions */
#ifdef ENABLE_STDIO
struct mrb_parser_state* mrb_parse_file(mrb_state*,FILE*,mrbc_context*);
#endif
struct mrb_parser_state* mrb_parse_string(mrb_state*,const char*,mrbc_context*);
struct mrb_parser_state* mrb_parse_nstring(mrb_state*,const char*,int,mrbc_context*);
int mrb_generate_code(mrb_state*, struct mrb_parser_state*);
/* program load functions */
#ifdef ENABLE_STDIO
mrb_value mrb_load_file(mrb_state*,FILE*);
#endif
mrb_value mrb_load_string(mrb_state *mrb, const char *s);
mrb_value mrb_load_nstring(mrb_state *mrb, const char *s, int len);
#ifdef ENABLE_STDIO
mrb_value mrb_load_file_cxt(mrb_state*,FILE*, mrbc_context *cxt);
#endif
mrb_value mrb_load_string_cxt(mrb_state *mrb, const char *s, mrbc_context *cxt);
mrb_value mrb_load_nstring_cxt(mrb_state *mrb, const char *s, int len, mrbc_context *cxt);
+11 -8
View File
@@ -1848,12 +1848,13 @@ codegen(codegen_scope *s, node *tree, int val)
case NODE_BACK_REF:
{
char buf[4];
int len;
char buf[2] = { '$' };
mrb_value str;
int sym;
len = snprintf(buf, sizeof(buf), "$%c", (int)(intptr_t)tree);
sym = new_sym(s, mrb_intern2(s->mrb, buf, len));
buf[1] = (char)(intptr_t)tree;
str = mrb_str_new(s->mrb, buf, 2);
sym = new_sym(s, mrb_intern_str(s->mrb, str));
genop(s, MKOP_ABx(OP_GETGLOBAL, cursp(), sym));
push();
}
@@ -1861,12 +1862,14 @@ codegen(codegen_scope *s, node *tree, int val)
case NODE_NTH_REF:
{
char buf[4];
int len;
int sym;
mrb_state *mrb = s->mrb;
mrb_value fix = mrb_fixnum_value((intptr_t)tree);
mrb_value str = mrb_str_buf_new(mrb, 4);
len = snprintf(buf, sizeof(buf), "$%d", (int)(intptr_t)tree);
sym = new_sym(s, mrb_intern2(s->mrb, buf, len));
mrb_str_buf_cat(mrb, str, "$", 1);
mrb_str_buf_append(mrb, str, mrb_fix2str(mrb, fix, 10));
sym = new_sym(s, mrb_intern_str(mrb, str));
genop(s, MKOP_ABx(OP_GETGLOBAL, cursp(), sym));
push();
}
+14 -2
View File
@@ -3241,12 +3241,15 @@ nextc(parser_state *p)
cons_free(tmp);
}
else {
#ifdef ENABLE_STDIO
if (p->f) {
if (feof(p->f)) return -1;
c = fgetc(p->f);
if (c == EOF) return -1;
}
else if (!p->s || p->s >= p->send) {
else
#endif
if (!p->s || p->s >= p->send) {
return -1;
}
else {
@@ -3304,6 +3307,7 @@ peeks(parser_state *p, const char *s)
{
int len = strlen(s);
#ifdef ENABLE_STDIO
if (p->f) {
int n = 0;
while (*s) {
@@ -3311,7 +3315,9 @@ peeks(parser_state *p, const char *s)
}
return TRUE;
}
else if (p->s && p->s + len >= p->send) {
else
#endif
if (p->s && p->s + len >= p->send) {
if (memcmp(p->s, s, len) == 0) return TRUE;
}
return FALSE;
@@ -5079,7 +5085,9 @@ mrb_parser_new(mrb_state *mrb)
p->in_def = p->in_single = 0;
p->s = p->send = NULL;
#ifdef ENABLE_STDIO
p->f = NULL;
#endif
p->cmd_start = TRUE;
p->in_def = p->in_single = FALSE;
@@ -5132,6 +5140,7 @@ mrbc_filename(mrb_state *mrb, mrbc_context *c, const char *s)
return c->filename;
}
#ifdef ENABLE_STDIO
parser_state*
mrb_parse_file(mrb_state *mrb, FILE *f, mrbc_context *c)
{
@@ -5145,6 +5154,7 @@ mrb_parse_file(mrb_state *mrb, FILE *f, mrbc_context *c)
mrb_parser_parse(p, c);
return p;
}
#endif
parser_state*
mrb_parse_nstring(mrb_state *mrb, const char *s, int len, mrbc_context *c)
@@ -5208,6 +5218,7 @@ load_exec(mrb_state *mrb, parser_state *p, mrbc_context *c)
return v;
}
#ifdef ENABLE_STDIO
mrb_value
mrb_load_file_cxt(mrb_state *mrb, FILE *f, mrbc_context *c)
{
@@ -5219,6 +5230,7 @@ mrb_load_file(mrb_state *mrb, FILE *f)
{
return mrb_load_file_cxt(mrb, f, NULL);
}
#endif
mrb_value
mrb_load_nstring_cxt(mrb_state *mrb, const char *s, int len, mrbc_context *c)
+1
View File
@@ -6,6 +6,7 @@
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>