Beispiel #1
0
/**
	result_get_length : 'result -> int
	<doc>Return the number of rows returned or affected</doc>
**/
static value result_get_length( value o ) {
	if( val_is_int(o) )
		return o;
	val_check_kind(o,k_result);
	return alloc_int( (int)mysql_num_rows(RESULT(o)->r) );
}
Beispiel #2
0
static value os_winlog_set( value wnd, value txt ) {
	val_check_kind(wnd,k_winlog);
	val_check(txt,string);
	sys_winlog_set(val_window(wnd)->p,val_string(txt));
	return val_null;
}
Beispiel #3
0
static value os_winlog_destroy( value wnd ) {
	val_check_kind(wnd,k_winlog);
	sys_winlog_destroy(val_window(wnd)->p);
	val_kind(wnd) = NULL;
	return val_null;
}
Beispiel #4
0
/**
	deque_push : 'deque -> any -> void
	<doc>add a message at the head of the queue</doc>
**/
static value deque_push( value v, value i ) {
	val_check_kind(v,k_deque);
	_deque_push(val_deque(v),i);
	return val_null;
}
Beispiel #5
0
/**
	deque_pop : 'deque -> bool -> any?
	<doc>pop a message from the queue head. Either block until a message is available or return immedialtly with null.</doc>
**/
static value deque_pop( value v, value block ) {
	val_check_kind(v,k_deque);
	val_check(block,bool);
	return _deque_pop(val_deque(v),val_bool(block));
}
Beispiel #6
0
/**
	file_eof : 'file -> bool
	<doc>Tell if we have reached the end of the file</doc>
**/
static value file_eof( value o ) {
	val_check_kind(o,k_file);
	return alloc_bool( feof(val_file(o)->io) );
}
Beispiel #7
0
/**
	$hsize : 'hash -> int
	<doc>Return the size of the hashtable</doc>
**/
static value builtin_hsize( value vh ) {
	val_check_kind(vh,k_hash);
	return alloc_int( val_hdata(vh)->ncells );
}
Beispiel #8
0
/**
	deflate_bound : 'dstream -> n:int -> int
	<doc>Return the maximum buffer size needed to write [n] bytes</doc>
**/
static value deflate_bound( value s, value size ) {
	val_check_kind(s,k_stream_def);
	val_check(size,int);
	return alloc_int(deflateBound(val_stream(s),val_int(size)));
}
Beispiel #9
0
/**
	mutex_acquire : 'mutex -> void
	<doc>
	The current thread acquire the mutex or wait if not available. The same thread can acquire
	several times the same mutex but must release it as many times it has been acquired.
	</doc>
**/
static value mutex_acquire( value m ) {
	val_check_kind(m,k_mutex);
	neko_lock_acquire( val_mutex(m) );
	return val_null;
}
Beispiel #10
0
/**
	inflate_end : 'istream -> void
	<doc>Close a decompression stream</doc>
**/
static value inflate_end( value s ) {
	val_check_kind(s,k_stream_inf);
	free_stream_inf(s);
	return alloc_null();
}
Beispiel #11
0
/**
	get_adler32 : 'stream -> 'int32
	<doc>Returns the adler32 value of the stream</doc>
**/
static value get_adler32( value s ) {
	if( !val_is_kind(s,k_stream_inf) )
		val_check_kind(s,k_stream_def);
	return alloc_int32(val_stream(s)->adler);
}
Beispiel #12
0
/**
 * Receive data from socket
 * Based on code in  https://github.com/zeromq/jzmq/blob/master/src/Socket.cpp
 */
value hx_zmq_rcv(value socket_handle_, value flags) {
	
	val_check_kind(socket_handle_, k_zmq_socket_handle);
	
	if (!val_is_null(flags) && !val_is_int(flags)) {
		val_throw(alloc_int(EINVAL));
		return alloc_null();
	}
	
	zmq_msg_t message;

    int rc = zmq_msg_init (&message);
    int err = zmq_errno();
    if (rc != 0) {
        val_throw(alloc_int(err));
        return alloc_null();
    }
	
	gc_enter_blocking();
	
#if ZMQ_VERSION >= ZMQ_MAKE_VERSION(3,0,0)	
    rc = zmq_recvmsg (val_data(socket_handle_), &message, val_int(flags));
#else
    rc = zmq_recv (val_data(socket_handle_), &message, val_int(flags));
#endif
	gc_exit_blocking();

    err = zmq_errno();
    if (rc == -1 && err == EAGAIN) {
        rc = zmq_msg_close (&message);
        err = zmq_errno();
        if (rc != 0) {
			val_throw(alloc_int(err));
			return alloc_null();
        }
        return alloc_null();
    }

    if (rc == -1) {
        rc = zmq_msg_close (&message);
        int err1 = zmq_errno();
        if (rc != 0) {
			val_throw(alloc_int(err1));
			return alloc_null();
        }
        val_throw(alloc_int(err));
        return alloc_null();
    }
	
	// Return data to Haxe
	int sz = zmq_msg_size (&message);
    const char* pd = (char *)zmq_msg_data (&message);

	// Create a return buffer byte array, by memcopying the message data, then discard the ZMQ message
	buffer b = alloc_buffer(NULL);
	buffer_append_sub(b,pd,sz);
	rc = zmq_msg_close (&message);
	err = zmq_errno();
	if (rc != 0) {
		val_throw(alloc_int(err));
		return alloc_null();
	}
	return buffer_val(b);
}
Beispiel #13
0
/**
 * Receive data from socket
 * Based on code in  https://github.com/zeromq/jzmq/blob/master/src/Socket.cpp
 */
value hx_zmq_send(value socket_handle_, value msg_data, value flags) {
	
	val_check_kind(socket_handle_, k_zmq_socket_handle);
	
	if (!val_is_null(flags) && !val_is_int(flags)) {
		val_throw(alloc_int(EINVAL));
		return alloc_null();
	}
	
	size_t size = 0;
	uint8_t *data = 0;
	
	
	zmq_msg_t message;

	// Extract byte data from either Neko string or C++ buffer
	// see: http://waxe.googlecode.com/svn-history/r32/trunk/src/waxe/HaxeAPI.cpp "Val2ByteData"
	if (val_is_string(msg_data))
	{
		// Neko
		size = val_strlen(msg_data);
		data = (uint8_t *)val_string(msg_data);
	}
	else if (val_is_buffer(msg_data))
	{
		// CPP
		buffer buf = val_to_buffer(msg_data);
		size = buffer_size(buf);
		data = (uint8_t *)buffer_data(buf);
	} else {
		val_throw(alloc_int(EINVAL));
		return alloc_null();
	}
		
	
	// Set up send message buffer by referencing the provided bytes data
    //zero copy version: int rc = zmq_msg_init_data (&message, data, size,NULL,NULL);
	int rc = zmq_msg_init_size(&message, size);
	memcpy (zmq_msg_data(&message), data, size);
	
    int err = zmq_errno();
    if (rc != 0) {
        val_throw(alloc_int(err));
        return alloc_null();
    }

	gc_enter_blocking();
	// Send
#if ZMQ_VERSION >= ZMQ_MAKE_VERSION(3,0,0)	
    rc = zmq_sendmsg (val_data(socket_handle_), &message, val_int(flags));
#else
    rc = zmq_send (val_data(socket_handle_), &message, val_int(flags));
#endif
    err = zmq_errno();
	
	gc_exit_blocking();
	
	// If NOBLOCK, but cant send message now, close message first before quitting
    if (rc == -1 && err == EAGAIN) {
        rc = zmq_msg_close (&message);
        err = zmq_errno();
        if (rc != 0) {
			val_throw(alloc_int(err));
			return alloc_null();
        }
        return alloc_null();
    }
    
    if (rc == -1) {
        val_throw(alloc_int(err));
        rc = zmq_msg_close (&message);
        err = zmq_errno();
        if (rc != 0) {
			val_throw(alloc_int(err));
			return alloc_null();
        }
        return alloc_null();
    }

    rc = zmq_msg_close (&message);
    err = zmq_errno();
    if (rc != 0) {
			val_throw(alloc_int(err));
			return alloc_null();
    }
	return alloc_null();
}
Beispiel #14
0
/**
	$iskind : any -> 'kind -> bool
	<doc>Tells if a value is of the given kind</doc>
**/
static value builtin_iskind( value v, value k ) {
	val_check_kind(k,neko_k_kind);
	return val_is_abstract(v) ? alloc_bool(val_kind(v) == (vkind)val_data(k)) : (val_data(k) == k_old_int32 ? alloc_bool(val_is_int32(v)) : val_false);
}
Beispiel #15
0
/**
	result_get_nfields : 'result -> int
	<doc>Return the number of fields in a result row</doc>
**/
static value result_get_nfields( value o ) {
	val_check_kind(o,k_result);
	return alloc_int(RESULT(o)->nfields);
}
Beispiel #16
0
/**
	mutex_try : 'mutex -> bool
	<doc>
	Try to acquire the mutex, returns true if acquire or false if it's already locked by another
	thread.
	</doc>
**/
static value mutex_try( value m ) {
	val_check_kind(m,k_mutex);
	return alloc_bool( neko_lock_try(val_mutex(m)) );	
}
Beispiel #17
0
/**
	result_next : 'result -> object?
	<doc>
	Return the next row if available. A row is represented
	as an object, which fields have been converted to the
	corresponding Neko value (int, float or string). For
	Date and DateTime you can specify your own conversion
	function using [result_set_conv_date]. By default they're
	returned as plain strings. Additionally, the TINYINT(1) will
	be converted to either true or false if equal to 0.
	</doc>
**/
static value result_next( value o ) {
	result *r;
	unsigned long *lengths = NULL;
	MYSQL_ROW row;
	val_check_kind(o,k_result);
	r = RESULT(o);
	row = mysql_fetch_row(r->r);
	if( row == NULL )
		return val_null;
	{
		int i;
		value cur = alloc_object(NULL);
		r->current = row;
		for(i=0;i<r->nfields;i++)
			if( row[i] != NULL ) {
				value v;
				switch( r->fields_convs[i] ) {
				case CONV_INT:
					v = alloc_int(atoi(row[i]));
					break;
				case CONV_STRING:
					v = alloc_string(row[i]);
					if( r->conv_string != NULL )
						v = val_call1(r->conv_string,v);
					break;
				case CONV_BOOL:
					v = alloc_bool( *row[i] != '0' );
					break;
				case CONV_FLOAT:
					v = alloc_float(atof(row[i]));
					break;
				case CONV_BINARY:
					if( lengths == NULL ) {
						lengths = mysql_fetch_lengths(r->r);
						if( lengths == NULL )
							val_throw(alloc_string("mysql_fetch_lengths"));
					}
					v = copy_string(row[i],lengths[i]);
					if( r->conv_bytes != NULL )
						v = val_call1(r->conv_bytes,v);
					break;
				case CONV_DATE:
					if( r->conv_date == NULL )
						v = alloc_string(row[i]);
					else {
						struct tm t;
						sscanf(row[i],"%4d-%2d-%2d",&t.tm_year,&t.tm_mon,&t.tm_mday);
						t.tm_hour = 0;
						t.tm_min = 0;
						t.tm_sec = 0;
						t.tm_isdst = -1;
						t.tm_year -= 1900;
						t.tm_mon--;
						v = val_call1(r->conv_date,alloc_int32((int)mktime(&t)));
					}
					break;
				case CONV_DATETIME:
					if( r->conv_date == NULL )
						v = alloc_string(row[i]);
					else {
						struct tm t;
						sscanf(row[i],"%4d-%2d-%2d %2d:%2d:%2d",&t.tm_year,&t.tm_mon,&t.tm_mday,&t.tm_hour,&t.tm_min,&t.tm_sec);
						t.tm_isdst = -1;
						t.tm_year -= 1900;
						t.tm_mon--;
						v = val_call1(r->conv_date,alloc_int32((int)mktime(&t)));
					}
					break;
				default:
					v = val_null;
					break;
				}
				alloc_field(cur,r->fields_ids[i],v);
			}
		return cur;
	}
}
Beispiel #18
0
/**
	mutex_release : 'mutex -> void
	<doc>
	Release a mutex that has been acquired by the current thread. The behavior is undefined if the
	current thread does not own the mutex.
	</doc>
**/
static value mutex_release( value m ) {
	val_check_kind(m,k_mutex);
	neko_lock_release(val_mutex(m));
	return val_null;
}
Beispiel #19
0
/**
	file_name : 'file -> string
	<doc>Return the name of the file which was opened</doc>
**/
static value file_name( value o ) {
	val_check_kind(o,k_file);
	return alloc_string(val_string(val_file(o)->name));
}
Beispiel #20
0
/**
	$hcount : 'hash -> int
	<doc>Return the number of elements in the hashtable</doc>
**/
static value builtin_hcount( value vh ) {
	val_check_kind(vh,k_hash);
	return alloc_int( val_hdata(vh)->nitems );
}