<commit_before>/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
 * vim: set ts=4 sw=4 et tw=99 ft=cpp:
 */

#include <assert.h>

#include "ejs-ops.h"
#include "ejs-value.h"
#include "ejs-function.h"
#include "ejs-date.h"

static ejsval  _ejs_date_specop_get (ejsval obj, ejsval propertyName, EJSBool isCStr);
static EJSPropertyDesc* _ejs_date_specop_get_own_property (ejsval obj, ejsval propertyName);
static EJSPropertyDesc* _ejs_date_specop_get_property (ejsval obj, ejsval propertyName);
static void    _ejs_date_specop_put (ejsval obj, ejsval propertyName, ejsval val, EJSBool flag);
static EJSBool _ejs_date_specop_can_put (ejsval obj, ejsval propertyName);
static EJSBool _ejs_date_specop_has_property (ejsval obj, ejsval propertyName);
static EJSBool _ejs_date_specop_delete (ejsval obj, ejsval propertyName, EJSBool flag);
static ejsval  _ejs_date_specop_default_value (ejsval obj, const char *hint);
static EJSBool _ejs_date_specop_define_own_property (ejsval obj, ejsval propertyName, EJSPropertyDesc* propertyDescriptor, EJSBool flag);
static void    _ejs_date_specop_finalize (EJSObject* obj);
static void    _ejs_date_specop_scan (EJSObject* obj, EJSValueFunc scan_func);

EJSSpecOps _ejs_date_specops = {
    "Date",
    _ejs_date_specop_get,
    _ejs_date_specop_get_own_property,
    _ejs_date_specop_get_property,
    _ejs_date_specop_put,
    _ejs_date_specop_can_put,
    _ejs_date_specop_has_property,
    _ejs_date_specop_delete,
    _ejs_date_specop_default_value,
    _ejs_date_specop_define_own_property,
    _ejs_date_specop_finalize,
    _ejs_date_specop_scan,
};

EJSObject* _ejs_date_alloc_instance()
{
    return (EJSObject*)_ejs_gc_new (EJSDate);
}

ejsval
_ejs_date_new_unix (int timestamp)
{
    EJSDate* rv = _ejs_gc_new (EJSDate);

    _ejs_init_object ((EJSObject*)rv, _ejs_Date_proto, &_ejs_date_specops);

    time_t t = (time_t)timestamp;

    if (!localtime_r(&t, &rv->tm))
        EJS_NOT_IMPLEMENTED();

    return OBJECT_TO_EJSVAL((EJSObject*)rv);
}

ejsval _ejs_Date;
ejsval _ejs_Date_proto;

static ejsval
_ejs_Date_impl (ejsval env, ejsval _this, int argc, ejsval *args)
{
    if (EJSVAL_IS_UNDEFINED(_this)) {
        // called as a function
        if (argc == 0) {
            return _ejs_date_new_unix(time(NULL));
        }
        else {
            EJS_NOT_IMPLEMENTED();
        }
    }
    else {
        printf ("called Date() as a constructor!\n");

        EJSDate* date = (EJSDate*) EJSVAL_TO_OBJECT(_this);

        // new Date (year, month [, date [, hours [, minutes [, seconds [, ms ] ] ] ] ] )

        if (argc <= 1) {
            time_t t = (time_t)time(NULL);

            if (!gmtime_r(&t, &date->tm))
                EJS_NOT_IMPLEMENTED();
        }
        else {
            // there are all sorts of validation steps here that are missing from ejs
            date->tm.tm_year = (int)(ToDouble(args[0]) - 1900);
            date->tm.tm_mon = (int)(ToDouble(args[1]));<commit_msg>remove spew<commit_after>/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
 * vim: set ts=4 sw=4 et tw=99 ft=cpp:
 */

#include <assert.h>

#include "ejs-ops.h"
#include "ejs-value.h"
#include "ejs-function.h"
#include "ejs-date.h"

static ejsval  _ejs_date_specop_get (ejsval obj, ejsval propertyName, EJSBool isCStr);
static EJSPropertyDesc* _ejs_date_specop_get_own_property (ejsval obj, ejsval propertyName);
static EJSPropertyDesc* _ejs_date_specop_get_property (ejsval obj, ejsval propertyName);
static void    _ejs_date_specop_put (ejsval obj, ejsval propertyName, ejsval val, EJSBool flag);
static EJSBool _ejs_date_specop_can_put (ejsval obj, ejsval propertyName);
static EJSBool _ejs_date_specop_has_property (ejsval obj, ejsval propertyName);
static EJSBool _ejs_date_specop_delete (ejsval obj, ejsval propertyName, EJSBool flag);
static ejsval  _ejs_date_specop_default_value (ejsval obj, const char *hint);
static EJSBool _ejs_date_specop_define_own_property (ejsval obj, ejsval propertyName, EJSPropertyDesc* propertyDescriptor, EJSBool flag);
static void    _ejs_date_specop_finalize (EJSObject* obj);
static void    _ejs_date_specop_scan (EJSObject* obj, EJSValueFunc scan_func);

EJSSpecOps _ejs_date_specops = {
    "Date",
    _ejs_date_specop_get,
    _ejs_date_specop_get_own_property,
    _ejs_date_specop_get_property,
    _ejs_date_specop_put,
    _ejs_date_specop_can_put,
    _ejs_date_specop_has_property,
    _ejs_date_specop_delete,
    _ejs_date_specop_default_value,
    _ejs_date_specop_define_own_property,
    _ejs_date_specop_finalize,
    _ejs_date_specop_scan,
};

EJSObject* _ejs_date_alloc_instance()
{
    return (EJSObject*)_ejs_gc_new (EJSDate);
}

ejsval
_ejs_date_new_unix (int timestamp)
{
    EJSDate* rv = _ejs_gc_new (EJSDate);

    _ejs_init_object ((EJSObject*)rv, _ejs_Date_proto, &_ejs_date_specops);

    time_t t = (time_t)timestamp;

    if (!localtime_r(&t, &rv->tm))
        EJS_NOT_IMPLEMENTED();

    return OBJECT_TO_EJSVAL((EJSObject*)rv);
}

ejsval _ejs_Date;
ejsval _ejs_Date_proto;

static ejsval
_ejs_Date_impl (ejsval env, ejsval _this, int argc, ejsval *args)
{
    if (EJSVAL_IS_UNDEFINED(_this)) {
        // called as a function
        if (argc == 0) {
            return _ejs_date_new_unix(time(NULL));
        }
        else {
            EJS_NOT_IMPLEMENTED();
        }
    }
    else {
        EJSDate* date = (EJSDate*) EJSVAL_TO_OBJECT(_this);

        // new Date (year, month [, date [, hours [, minutes [, seconds [, ms ] ] ] ] ] )

        if (argc <= 1) {
            time_t t = (time_t)time(NULL);

            if (!gmtime_r(&t, &date->tm))
                EJS_NOT_IMPLEMENTED();
        }
        else {
            // there are all sorts of validation steps here that are missing from ejs
            date->tm.tm_year = (int)(ToDouble(args[0]) - 1900);
            date->tm.tm_mon = (int)(ToDouble(args[1]));
{'fraction_non_alphanumeric': 0.10323783692151212, 'fraction_numerical': 0.004746567214782166, 'mean_word_length': 3.828150572831424, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 13, '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': '6111568', 'n_tokens_mistral': 2323, 'n_tokens_neox': 2303, 'n_words': 456}