/*
 * Ruby/JS 
 * Copyright (c) 2003 TAMURA.Kenichi <tamura@ruby-lang.org>
 *
 * NGS JavaScript interpreter Engine for using Ruby extension
 * The NGS JavaScript is a GPL free interpreter for the 
 * JavaScript language. 
 * WWW Home Page http://www.ngs.fi/js/
 */
/*
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the Free
 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
 * MA 02111-1307, USA
 */

#include "ruby.h"
#include "js.h"
#include "shim16to18.h"

static VALUE cJS;
static VALUE eJSError;

VALUE
rb_js_convertvalue(JSInterpPtr interp, JSType value)
{
  VALUE ary;
  int i;

  switch (value.type)
    {
    case JS_TYPE_UNDEFINED:
    case JS_TYPE_NULL:
      return Qnil;
    case JS_TYPE_BOOLEAN:
      return value.u.i ? Qtrue : Qfalse;
    case JS_TYPE_INTEGER:
      return INT2NUM (value.u.i);
    case JS_TYPE_STRING:
      return rb_str_new ((JSCharPtr) value.u.s->data,
                               value.u.s->len);
    case JS_TYPE_DOUBLE:
      return rb_float_new (value.u.d);
    case JS_TYPE_ARRAY:
      ary = rb_ary_new2(value.u.array->length);

      for (i = 0; i < value.u.array->length; i++) {
        VALUE o = rb_js_convertvalue (interp, 
                        value.u.array->data[i]);
        rb_ary_store (ary, i, o);
      }
      return ary;
//  case JS_TYPE_BUILTIN:
    default:
      rb_raise (eJSError, "not supported type");
      break;
    }
}

VALUE
rb_js_evalcode(JSInterpPtr interp, VALUE code)
{
  JSType result;

  SafeStringValue (code);

  if (!js_eval (interp, StringValuePtr (code)))
    rb_raise (eJSError, js_error_message (interp));

  js_result (interp, &result);
  return rb_obj_taint(rb_js_convertvalue(interp, result));
}

static VALUE
rb_js_evaluate (VALUE obj, VALUE code)
{
  JSInterpPtr interp;
  VALUE r;

  interp = (JSInterpPtr) js_create_interp (NULL);
  if (interp == NULL)
    rb_raise (eJSError, "Fail: create_interp();");

  r = rb_js_evalcode(interp, code);

  js_destroy_interp (interp);
  return r;
}

static VALUE
rb_js_new (VALUE class)
{
  JSInterpPtr interp;

  interp = (JSInterpPtr) js_create_interp (NULL);
  if (interp == NULL)
    rb_raise (eJSError, "Fail: create_interp();");

  return Data_Wrap_Struct(class, 0, js_destroy_interp, (void *)interp);
}

static VALUE
rb_js_eval(VALUE obj, VALUE code)
{
  JSInterpPtr interp;
  Data_Get_Struct(obj, JSInterpPtr, (void *) interp);
  return rb_js_evalcode(interp, code);
}

void
Init_js ()
{
  cJS = rb_define_class("JS", rb_cObject);
  rb_define_const (cJS, "LIB_VERSION",
		   rb_obj_freeze (rb_str_new2 (js_version ())));

  rb_define_singleton_method(cJS, "evaluate", rb_js_evaluate, 1);
  rb_define_singleton_method(cJS, "new", rb_js_new, 0);
  rb_define_method(cJS, "eval", rb_js_eval, 1);

  eJSError = rb_define_class ("JSError", rb_eStandardError);
}
