/* -*- mode: c ; c-file-style: "canonware-c-style" -*-
 ******************************************************************************
 *
 * Copyright (C) 1996-2004 Jason Evans <jasone@canonware.com>.
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice(s), this list of conditions and the following disclaimer
 *    unmodified other than the allowable addition of one or more
 *    copyright notices.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice(s), this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 ******************************************************************************
 *
 * Version: Onyx 5.1.1
 *
 ******************************************************************************/

#include "../include/libonyx/libonyx.h"

#define NAME_anon_handle "anon_handle"
#define NAME_mark_handle "mark_handle"

const char *data = "handle data";

void
handle_eval(void *a_data, cw_nxo_t *a_thread)
{
    cw_assert(a_data == data);

    cw_onyx_code(a_thread, "`Evaluated handle\n' print");
}

cw_nxoe_t *
handle_ref_iter(void *a_data, bool a_reset)
{
    cw_assert(a_data == data);

    return NULL;
}

bool
handle_delete(void *a_data, uint32_t a_iter)
{
    cw_assert(a_data == data);

    return false;
}

void
anon_handle(cw_nxo_t *a_thread)
{
    cw_nxo_t *ostack, *handle;

    ostack = nxo_thread_ostack_get(a_thread);
    handle = nxo_stack_push(ostack);
    nxo_handle_new(handle, (void *) data, handle_eval, handle_ref_iter,
		   handle_delete);
    nxo_attr_set(handle, NXOA_EXECUTABLE);
}

void
mark_handle(cw_nxo_t *a_thread)
{
    cw_nxo_t *ostack, *handle, *tag;

    anon_handle(a_thread);

    ostack = nxo_thread_ostack_get(a_thread);
    handle = nxo_stack_get(ostack);
    tag = nxo_handle_tag_get(handle);
    nxo_mark_new(tag);
}

int
main(int argc, char **argv, char **envp)
{
    cw_nx_t nx;
    cw_nxo_t thread, *ostack, *name, *operator;

    libonyx_init(argc, argv, envp);
    fprintf(stderr, "Test begin\n");

    cw_assert(nx_new(&nx, NULL, NULL) == &nx);
    nxo_thread_new(&thread, &nx);

    /* Define anon_handle and mark_handle. */
    ostack = nxo_thread_ostack_get(&thread);

    name = nxo_stack_push(ostack);
    operator = nxo_stack_push(ostack);
    nxo_name_new(name, NAME_anon_handle, strlen(NAME_anon_handle), false);
    nxo_operator_new(operator, anon_handle, NXN_ZERO);
    nxo_attr_set(operator, NXOA_EXECUTABLE);
    cw_onyx_code(&thread, "def");

    name = nxo_stack_push(ostack);
    operator = nxo_stack_push(ostack);
    nxo_name_new(name, NAME_mark_handle, strlen(NAME_mark_handle), false);
    nxo_operator_new(operator, mark_handle, NXN_ZERO);
    nxo_attr_set(operator, NXOA_EXECUTABLE);
    cw_onyx_code(&thread, "def");

    cw_onyx_code(&thread, "\n\
anon_handle 1 sprint\n\
anon_handle eval\n\
mark_handle 1 sprint\n\
mark_handle eval\n\
");
    nx_delete(&nx);

    libonyx_shutdown();
    fprintf(stderr, "Test end\n");
    return 0;
}
