<commit_before>/* Swfdec
 * Copyright (C) 2003-2006 <NAME> <<EMAIL>>
 *		 2005-2006 <NAME> <<EMAIL>>
 *		      2006 <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 <string.h>
#include <math.h>

#include <js/jsapi.h>
#include "swfdec_js.h"
#include "swfdec_movie.h"
#include "swfdec_bits.h"
#include "swfdec_debug.h"
#include "swfdec_decoder.h"
#include "swfdec_player_internal.h"
#include "swfdec_root_movie.h"
#include "swfdec_sprite.h"
#include "swfdec_sprite_movie.h"

JSBool swfdec_js_eval (JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval);

static void
movie_finalize (JSContext *cx, JSObject *obj)
{
  SwfdecMovie *movie;

  movie = JS_GetPrivate (cx, obj);
  /* since we also finalize the class, not everyone has a private object */
  if (movie) {
    g_assert (movie->jsobj != NULL);

    SWFDEC_LOG ("destroying JSObject %p for movie %p", obj, movie);
    movie->jsobj = NULL;
    g_object_unref (movie);
  }
}

static JSClass movieclip_class = {
    "Movie", JSCLASS_NEW_RESOLVE | JSCLASS_HAS_PRIVATE,
    JS_PropertyStub,  JS_PropertyStub,
    JS_PropertyStub,  JS_PropertyStub,
    JS_EnumerateStub, JS_ResolveStub,
    JS_ConvertStub,   movie_finalize,
};

static JSBool
mc_play (JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
  SwfdecMovie *movie;

  movie = JS_GetPrivate(cx, obj);
  g_assert (movie);
  movie->stopped = FALSE;

  return JS_TRUE;
}

static JSBool
mc_stop (JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
  SwfdecMovie *movie;

  movie = JS_GetPrivate(cx, obj);
  g_assert (movie);
  movie->stopped = TRUE;

  return JS_TRUE;
}

static JSBool
mc_getBytesLoaded (JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
  SwfdecMovie *movie;
  SwfdecDecoder *dec;

  movie = JS_GetPrivate(cx, obj);
  dec = SWFDEC_ROOT_MOVIE (movie->root)->decoder;

  *rval = INT_TO_JSVAL(MIN (dec->bytes_loaded, dec->bytes_total));

  return JS_TRUE;
}

static JSBool
mc_getBytesTotal(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
  SwfdecMovie *movie;
  SwfdecDecoder *dec;

  movie = JS_GetPrivate(cx, obj);
  dec = SWFDEC_ROOT_MOVIE (movie->root)->decoder;

  *rval = INT_TO_JSVAL (dec->bytes_total);

  return JS_TRUE;
}

static JSBool
mc_do_goto (JSContext *cx, SwfdecMovie *movie, jsval target)
{
  int32 frame;

  if (JSVAL_IS_STRING (target)) {
    const char *label = swfdec_js_to_string (cx, target);
    frame = swfdec_sprite_get_frame (SWFDEC_SPRITE_MOVIE (movie)->sprite, label);
    /* FIXME: nonexisting frames? */
    if (frame == -1)
      return JS_FALSE;
    frame++;
  } else if (!JS_ValueToInt32 (cx, target, &frame)) {
    return JS_FALSE;
  }
  /* FIXME: how to handle overflow? */
  frame = CLAMP (frame, 1, (int) movie->n_frames) - 1;

  swfdec_movie_goto (movie, frame);
  return JS_TRUE;
}

static JSBool
mc_gotoAndPlay (JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
  SwfdecMovie *movie;

  movie = JS_GetPrivate(cx, obj);
  g_assert (movie);
  
  if (!mc_do_goto (cx, movie, argv[0]))
    return JS_FALSE;
  movie->stopped = FALSE;
  return JS_TRUE;
}

static JSBool
mc_gotoAndStop (JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
  SwfdecMovie *movie;

  movie = JS_GetPrivate(cx, obj);
  g_assert (movie);
  
  if (!mc_do_goto (cx, movie, argv[0]))
    return JS_FALSE;
  movie->stopped = TRUE;
  return JS_TRUE;
}

static JSBool
mc_hitTest (JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
  SwfdecMovie *movie;

  movie = JS_GetPrivate(cx, obj);
  g_assert (movie);
  
  if (argc == 1) {
    SwfdecMovie *other;
    if (!JSVAL_IS_OBJECT (argv[0]) ||
	JS_GetClass (JSVAL_TO_OBJECT (argv[0])) != &movieclip_class) {
      g_assert_not_reached ();
      return JS_FALSE;
    }
    other = SWFDEC_MOVIE (JS_GetPrivate(cx, JSVAL_TO_OBJECT (argv[0])));
    swfdec_movie_update (movie);
    swfdec_movie_update (other);
    /* FIXME */
    g_assert (movie->parent == other->parent);
#if 0
    g_print ("%g %g  %g %g --- %g %g  %g %g\n", 
	SWFDEC_OBJECT (movie)->extents.x0, SWFDEC_OBJECT (movie)->extents.y0,
	SWFDEC_OBJECT (movie)->extents.x1, SWFDEC_OBJECT (movie)->extents.y1,
	SWFDEC_OBJECT (other)->extents.x0, SWFDEC_OBJECT (other)->extents.y0,
	SWFDEC_OBJECT (other)->extents.x1, SWFDEC_OBJECT (other)->extents.y1);
#endif
    if (swfdec_rect_intersect (NULL, &movie->extents, &other->extents)) {
      *rval = BOOLEAN_TO_JSVAL (JS_TRUE);
    } else {
      *rval = BOOLEAN_TO_JSVAL (JS_FALSE);
    }
  } else if (argc == 3) {
    g_assert_not_reached ();
  } else {
    return JS_FALSE;
  }

  return JS_TRUE;
}

/* FIXME: replace with eval */
static SwfdecMovie *
get_target (SwfdecMovie *movie, const char *target, JSBool case_sensitive)
{
  char *tmp;
  guint len;
  GList *walk;

  //g_print ("get_target: %s\n", target);
  if (target[0] == '\0')
    return movie;

  if (g_str_has_prefix (target, "../")) {
    if (movie->parent == NULL)
      return NULL;
    return get_target (movie->parent, target + 3, case_sensitive);
  }
  tmp = strchr (target, '/');
  if (tmp)
    len = tmp - target;
  else
    len = strlen (target);

  for (walk = movie->list; walk; walk = walk->next) {
    SwfdecMovie *cur = walk->data;
    if (cur->content->name) {
      if ((case_sensitive && strncmp (cur->content->name, target, len) == 0) || 
	  (!case_sensitive && g_ascii_strncasecmp (cur->content->name, target, len) == 0)) {
	if (target[len] == '\0')
	  return cur;
	else 
	  return get_target (cur, target + len + 1, case_sensitive);
      }
    }
  }
  return NULL;
}

extern  JSPropertySpec movieclip_props[];

static JSBool
swfdec_js_getProperty (JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
  uint32 id;
  SwfdecMovie *movie;

  if (JSVAL_IS_OBJECT (argv[0])) {
    movie = JS_GetPrivate(cx, JSVAL_TO_OBJECT (argv[0]));
  } else if (JSVAL_IS_STRING (argv[0])) {
    char *str = JS_GetStringBytes (JSVAL_TO_STRING (argv[0]));
    movie = get_target (movie, str, JS_GetContextCaseSensitive (cx));
    if (movie == NULL) {
      SWFDEC_INFO ("no target name \"%s\"", str);
      return JS_FALSE;
    }
  } else {
    return JS_FALSE;
  }
  if (!JS_ValueToECMAUint32 (cx, argv[1], &id))
    return JS_FALSE;

  if (id > 19)
    return JS_FALSE;

  if (movie->jsobj == NULL &&
      !swfdec_js_add_movie (movie))
    return JS_FALSE;
  return movieclip_props[id].getter (cx, movie->jsobj, JSVAL_VOID /* FIXME */, rval);
}

static JSBool
swfdec_js_setProperty (JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
  uint32 id;
  SwfdecMovie *movie;

  if (JSVAL_IS_OBJECT (argv[0])) {
    movie = JS_GetPrivate(cx, JSVAL_TO_OBJECT (argv[0]));
  } else if (JSVAL_IS_STRING (argv[0])) {<commit_msg>whoops, that line must have been accidently deleted<commit_after>/* Swfdec
 * Copyright (C) 2003-2006 <NAME> <<EMAIL>>
 *		 2005-2006 <NAME> <<EMAIL>>
 *		      2006 <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 <string.h>
#include <math.h>

#include <js/jsapi.h>
#include "swfdec_js.h"
#include "swfdec_movie.h"
#include "swfdec_bits.h"
#include "swfdec_debug.h"
#include "swfdec_decoder.h"
#include "swfdec_player_internal.h"
#include "swfdec_root_movie.h"
#include "swfdec_sprite.h"
#include "swfdec_sprite_movie.h"

JSBool swfdec_js_eval (JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval);

static void
movie_finalize (JSContext *cx, JSObject *obj)
{
  SwfdecMovie *movie;

  movie = JS_GetPrivate (cx, obj);
  /* since we also finalize the class, not everyone has a private object */
  if (movie) {
    g_assert (movie->jsobj != NULL);

    SWFDEC_LOG ("destroying JSObject %p for movie %p", obj, movie);
    movie->jsobj = NULL;
    g_object_unref (movie);
  }
}

static JSClass movieclip_class = {
    "Movie", JSCLASS_NEW_RESOLVE | JSCLASS_HAS_PRIVATE,
    JS_PropertyStub,  JS_PropertyStub,
    JS_PropertyStub,  JS_PropertyStub,
    JS_EnumerateStub, JS_ResolveStub,
    JS_ConvertStub,   movie_finalize,
};

static JSBool
mc_play (JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
  SwfdecMovie *movie;

  movie = JS_GetPrivate(cx, obj);
  g_assert (movie);
  movie->stopped = FALSE;

  return JS_TRUE;
}

static JSBool
mc_stop (JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
  SwfdecMovie *movie;

  movie = JS_GetPrivate(cx, obj);
  g_assert (movie);
  movie->stopped = TRUE;

  return JS_TRUE;
}

static JSBool
mc_getBytesLoaded (JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
  SwfdecMovie *movie;
  SwfdecDecoder *dec;

  movie = JS_GetPrivate(cx, obj);
  dec = SWFDEC_ROOT_MOVIE (movie->root)->decoder;

  *rval = INT_TO_JSVAL(MIN (dec->bytes_loaded, dec->bytes_total));

  return JS_TRUE;
}

static JSBool
mc_getBytesTotal(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
  SwfdecMovie *movie;
  SwfdecDecoder *dec;

  movie = JS_GetPrivate(cx, obj);
  dec = SWFDEC_ROOT_MOVIE (movie->root)->decoder;

  *rval = INT_TO_JSVAL (dec->bytes_total);

  return JS_TRUE;
}

static JSBool
mc_do_goto (JSContext *cx, SwfdecMovie *movie, jsval target)
{
  int32 frame;

  if (JSVAL_IS_STRING (target)) {
    const char *label = swfdec_js_to_string (cx, target);
    frame = swfdec_sprite_get_frame (SWFDEC_SPRITE_MOVIE (movie)->sprite, label);
    /* FIXME: nonexisting frames? */
    if (frame == -1)
      return JS_FALSE;
    frame++;
  } else if (!JS_ValueToInt32 (cx, target, &frame)) {
    return JS_FALSE;
  }
  /* FIXME: how to handle overflow? */
  frame = CLAMP (frame, 1, (int) movie->n_frames) - 1;

  swfdec_movie_goto (movie, frame);
  return JS_TRUE;
}

static JSBool
mc_gotoAndPlay (JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
  SwfdecMovie *movie;

  movie = JS_GetPrivate(cx, obj);
  g_assert (movie);
  
  if (!mc_do_goto (cx, movie, argv[0]))
    return JS_FALSE;
  movie->stopped = FALSE;
  return JS_TRUE;
}

static JSBool
mc_gotoAndStop (JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
  SwfdecMovie *movie;

  movie = JS_GetPrivate(cx, obj);
  g_assert (movie);
  
  if (!mc_do_goto (cx, movie, argv[0]))
    return JS_FALSE;
  movie->stopped = TRUE;
  return JS_TRUE;
}

static JSBool
mc_hitTest (JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
  SwfdecMovie *movie;

  movie = JS_GetPrivate(cx, obj);
  g_assert (movie);
  
  if (argc == 1) {
    SwfdecMovie *other;
    if (!JSVAL_IS_OBJECT (argv[0]) ||
	JS_GetClass (JSVAL_TO_OBJECT (argv[0])) != &movieclip_class) {
      g_assert_not_reached ();
      return JS_FALSE;
    }
    other = SWFDEC_MOVIE (JS_GetPrivate(cx, JSVAL_TO_OBJECT (argv[0])));
    swfdec_movie_update (movie);
    swfdec_movie_update (other);
    /* FIXME */
    g_assert (movie->parent == other->parent);
#if 0
    g_print ("%g %g  %g %g --- %g %g  %g %g\n", 
	SWFDEC_OBJECT (movie)->extents.x0, SWFDEC_OBJECT (movie)->extents.y0,
	SWFDEC_OBJECT (movie)->extents.x1, SWFDEC_OBJECT (movie)->extents.y1,
	SWFDEC_OBJECT (other)->extents.x0, SWFDEC_OBJECT (other)->extents.y0,
	SWFDEC_OBJECT (other)->extents.x1, SWFDEC_OBJECT (other)->extents.y1);
#endif
    if (swfdec_rect_intersect (NULL, &movie->extents, &other->extents)) {
      *rval = BOOLEAN_TO_JSVAL (JS_TRUE);
    } else {
      *rval = BOOLEAN_TO_JSVAL (JS_FALSE);
    }
  } else if (argc == 3) {
    g_assert_not_reached ();
  } else {
    return JS_FALSE;
  }

  return JS_TRUE;
}

/* FIXME: replace with eval */
static SwfdecMovie *
get_target (SwfdecMovie *movie, const char *target, JSBool case_sensitive)
{
  char *tmp;
  guint len;
  GList *walk;

  //g_print ("get_target: %s\n", target);
  if (target[0] == '\0')
    return movie;

  if (g_str_has_prefix (target, "../")) {
    if (movie->parent == NULL)
      return NULL;
    return get_target (movie->parent, target + 3, case_sensitive);
  }
  tmp = strchr (target, '/');
  if (tmp)
    len = tmp - target;
  else
    len = strlen (target);

  for (walk = movie->list; walk; walk = walk->next) {
    SwfdecMovie *cur = walk->data;
    if (cur->content->name) {
      if ((case_sensitive && strncmp (cur->content->name, target, len) == 0) || 
	  (!case_sensitive && g_ascii_strncasecmp (cur->content->name, target, len) == 0)) {
	if (target[len] == '\0')
	  return cur;
	else 
	  return get_target (cur, target + len + 1, case_sensitive);
      }
    }
  }
  return NULL;
}

extern  JSPropertySpec movieclip_props[];

static JSBool
swfdec_js_getProperty (JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
  uint32 id;
  SwfdecMovie *movie;

  if (JSVAL_IS_OBJECT (argv[0])) {
    movie = JS_GetPrivate(cx, JSVAL_TO_OBJECT (argv[0]));
  } else if (JSVAL_IS_STRING (argv[0])) {
    char *str = JS_GetStringBytes (JSVAL_TO_STRING (argv[0]));
    movie = JS_GetPrivate(cx, obj);
    movie = get_target (movie, str, JS_GetContextCaseSensitive (cx));
    if (movie == NULL) {
      SWFDEC_INFO ("no target name \"%s\"", str);
      return JS_FALSE;
    }
  } else {
    return JS_FALSE;
  }
  if (!JS_ValueToECMAUint32 (cx, argv[1], &id))
    return JS_FALSE;

  if (id > 19)
    return JS_FALSE;

  if (movie->jsobj == NULL &&
      !swfdec_js_add_movie (movie))
    return JS_FALSE;
  return movieclip_props[id].getter (cx, movie->jsobj, JSVAL_VOID /* FIXME */, rval);
}

static JSBool
swfdec_js_setProperty (JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
  uint32 id;
  SwfdecMovie *movie;

  if (JSVAL_IS_OBJECT (argv[0])) {
    movie = JS_GetPrivate(cx, JSVAL_TO_OBJECT (argv[0]));
  } else if (JSVAL_IS_STRING (argv[0])) {
{'fraction_non_alphanumeric': 0.13190589479249273, 'fraction_numerical': 0.010441448585778482, 'mean_word_length': 3.8503205128205127, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 109, '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': '7191809', 'n_tokens_mistral': 5916, 'n_tokens_neox': 5673, 'n_words': 1518}