<commit_before>/* Swfdec
 * Copyright (C) 2007 <NAME> <<EMAIL>>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 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
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, 
 * Boston, MA  02110-1301  USA
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <math.h>

#include "swfdec_as_number.h"
#include "swfdec_as_context.h"
#include "swfdec_as_frame.h"
#include "swfdec_as_native_function.h"
#include "swfdec_as_strings.h"
#include "swfdec_debug.h"

G_DEFINE_TYPE (SwfdecAsNumber, swfdec_as_number, SWFDEC_TYPE_AS_OBJECT)

static void
swfdec_as_number_class_init (SwfdecAsNumberClass *klass)
{
}

static void
swfdec_as_number_init (SwfdecAsNumber *number)
{
}

/**
 * swfdec_as_number_new:
 * @context: a #SwfdecAsContext
 * @number: value of the number
 *
 * Creates a new #SwfdecAsNumber. This is the same as executing the Actionscript
 * code "new Number ()"
 *
 * Returns: the new number or %NULL on OOM.
 **/
SwfdecAsObject *
swfdec_as_number_new (SwfdecAsContext *context, double number)
{
  SwfdecAsObject *ret;
  SwfdecAsValue val;

  g_return_val_if_fail (SWFDEC_IS_AS_CONTEXT (context), NULL);
  g_return_val_if_fail (context->Number != NULL, NULL);
  
  SWFDEC_AS_VALUE_SET_NUMBER (&val, number);
  ret = swfdec_as_object_create (SWFDEC_AS_FUNCTION (context->Number), 1, &val, FALSE);
  swfdec_as_context_run (context);
  return ret;
}

/*** AS CODE ***/

static void
swfdec_as_number_construct (SwfdecAsContext *cx, SwfdecAsObject *object,
    guint argc, SwfdecAsValue *argv, SwfdecAsValue *ret)
{
  double d;

  if (argc > 0) {
    d = swfdec_as_value_to_number (object->context, &argv[0]);
  } else {
    d = NAN;
  }

  if (cx->frame->construct) {
    SwfdecAsNumber *num = SWFDEC_AS_NUMBER (object);
    num->number = d;
    SWFDEC_AS_VALUE_SET_OBJECT (ret, object);
  } else {
    SWFDEC_AS_VALUE_SET_NUMBER (ret, d);
  }
}

static void
swfdec_as_number_toString (SwfdecAsContext *cx, SwfdecAsObject *object,
    guint argc, SwfdecAsValue *argv, SwfdecAsValue *ret)
{
  SwfdecAsNumber *num = SWFDEC_AS_NUMBER (object);
  SwfdecAsValue val;
  const char *s;
  
  if (argc > 0) {
    SWFDEC_FIXME ("radix is not yet implemented");
  }
  SWFDEC_AS_VALUE_SET_NUMBER (&val, num->number);
  s = swfdec_as_value_to_string (object->context, &val);
  SWFDEC_AS_VALUE_SET_STRING (ret, s);
}

static void
swfdec_as_number_valueOf (SwfdecAsContext *cx, SwfdecAsObject *object,
    guint argc, SwfdecAsValue *argv, SwfdecAsValue *ret)
{
  SwfdecAsNumber *num = SWFDEC_AS_NUMBER (object);

  SWFDEC_AS_VALUE_SET_NUMBER (ret, num->number);
}

void
swfdec_as_number_init_context (SwfdecAsContext *context, guint version)
{
  SwfdecAsObject *number, *proto;
  SwfdecAsValue val;

  g_return_if_fail (SWFDEC_IS_AS_CONTEXT (context));

  number = SWFDEC_AS_OBJECT (swfdec_as_object_add_function (context->global,
      SWFDEC_AS_STR_Number, SWFDEC_TYPE_AS_NUMBER, swfdec_as_number_construct, 0));
  swfdec_as_native_function_set_construct_type (SWFDEC_AS_NATIVE_FUNCTION (number), SWFDEC_TYPE_AS_NUMBER);
  if (!number)
    return;
  context->Number = number;
  swfdec_as_native_function_set_object_type (SWFDEC_AS_NATIVE_FUNCTION (number), 
      SWFDEC_TYPE_AS_NUMBER);
  proto = swfdec_as_object_new (context);<commit_msg>don't call functions on objects that may be NULL<commit_after>/* Swfdec
 * Copyright (C) 2007 <NAME> <<EMAIL>>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 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
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, 
 * Boston, MA  02110-1301  USA
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <math.h>

#include "swfdec_as_number.h"
#include "swfdec_as_context.h"
#include "swfdec_as_frame.h"
#include "swfdec_as_native_function.h"
#include "swfdec_as_strings.h"
#include "swfdec_debug.h"

G_DEFINE_TYPE (SwfdecAsNumber, swfdec_as_number, SWFDEC_TYPE_AS_OBJECT)

static void
swfdec_as_number_class_init (SwfdecAsNumberClass *klass)
{
}

static void
swfdec_as_number_init (SwfdecAsNumber *number)
{
}

/**
 * swfdec_as_number_new:
 * @context: a #SwfdecAsContext
 * @number: value of the number
 *
 * Creates a new #SwfdecAsNumber. This is the same as executing the Actionscript
 * code "new Number ()"
 *
 * Returns: the new number or %NULL on OOM.
 **/
SwfdecAsObject *
swfdec_as_number_new (SwfdecAsContext *context, double number)
{
  SwfdecAsObject *ret;
  SwfdecAsValue val;

  g_return_val_if_fail (SWFDEC_IS_AS_CONTEXT (context), NULL);
  g_return_val_if_fail (context->Number != NULL, NULL);
  
  SWFDEC_AS_VALUE_SET_NUMBER (&val, number);
  ret = swfdec_as_object_create (SWFDEC_AS_FUNCTION (context->Number), 1, &val, FALSE);
  swfdec_as_context_run (context);
  return ret;
}

/*** AS CODE ***/

static void
swfdec_as_number_construct (SwfdecAsContext *cx, SwfdecAsObject *object,
    guint argc, SwfdecAsValue *argv, SwfdecAsValue *ret)
{
  double d;

  if (argc > 0) {
    d = swfdec_as_value_to_number (object->context, &argv[0]);
  } else {
    d = NAN;
  }

  if (cx->frame->construct) {
    SwfdecAsNumber *num = SWFDEC_AS_NUMBER (object);
    num->number = d;
    SWFDEC_AS_VALUE_SET_OBJECT (ret, object);
  } else {
    SWFDEC_AS_VALUE_SET_NUMBER (ret, d);
  }
}

static void
swfdec_as_number_toString (SwfdecAsContext *cx, SwfdecAsObject *object,
    guint argc, SwfdecAsValue *argv, SwfdecAsValue *ret)
{
  SwfdecAsNumber *num = SWFDEC_AS_NUMBER (object);
  SwfdecAsValue val;
  const char *s;
  
  if (argc > 0) {
    SWFDEC_FIXME ("radix is not yet implemented");
  }
  SWFDEC_AS_VALUE_SET_NUMBER (&val, num->number);
  s = swfdec_as_value_to_string (object->context, &val);
  SWFDEC_AS_VALUE_SET_STRING (ret, s);
}

static void
swfdec_as_number_valueOf (SwfdecAsContext *cx, SwfdecAsObject *object,
    guint argc, SwfdecAsValue *argv, SwfdecAsValue *ret)
{
  SwfdecAsNumber *num = SWFDEC_AS_NUMBER (object);

  SWFDEC_AS_VALUE_SET_NUMBER (ret, num->number);
}

void
swfdec_as_number_init_context (SwfdecAsContext *context, guint version)
{
  SwfdecAsObject *number, *proto;
  SwfdecAsValue val;

  g_return_if_fail (SWFDEC_IS_AS_CONTEXT (context));

  number = SWFDEC_AS_OBJECT (swfdec_as_object_add_function (context->global,
      SWFDEC_AS_STR_Number, SWFDEC_TYPE_AS_NUMBER, swfdec_as_number_construct, 0));
  if (!number)
    return;
  swfdec_as_native_function_set_construct_type (SWFDEC_AS_NATIVE_FUNCTION (number), SWFDEC_TYPE_AS_NUMBER);
  context->Number = number;
  swfdec_as_native_function_set_object_type (SWFDEC_AS_NATIVE_FUNCTION (number), 
      SWFDEC_TYPE_AS_NUMBER);
  proto = swfdec_as_object_new (context);
{'fraction_non_alphanumeric': 0.09429569266589057, 'fraction_numerical': 0.005691372396843876, 'mean_word_length': 4.9706563706563704, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2461476', 'n_tokens_mistral': 2869, 'n_tokens_neox': 2813, 'n_words': 729}