/* Automatically generated by
	VMPluginCodeGenerator VMMaker.oscog-eem.2480 uuid: bb3ffda7-8241-4dea-b886-d656e474b6c1
   from
	WeDoPlugin VMMaker.oscog-eem.2480 uuid: bb3ffda7-8241-4dea-b886-d656e474b6c1
 */
static char __buildInfo[] = "WeDoPlugin VMMaker.oscog-eem.2480 uuid: bb3ffda7-8241-4dea-b886-d656e474b6c1 " __DATE__ ;



#include "config.h"
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

/* Default EXPORT macro that does nothing (see comment in sq.h): */
#define EXPORT(returnType) returnType

/* Do not include the entire sq.h file but just those parts needed. */
#include "sqConfig.h"			/* Configuration options */
#include "sqVirtualMachine.h"	/*  The virtual machine proxy definition */
#include "sqPlatformSpecific.h"	/* Platform specific definitions */

#define true 1
#define false 0
#define null 0  /* using 'null' because nil is predefined in Think C */
#ifdef SQUEAK_BUILTIN_PLUGIN
# undef EXPORT
# define EXPORT(returnType) static returnType
#endif

#include "WeDoPlugin.h"
#include "sqMemoryAccess.h"


/*** Function Prototypes ***/
EXPORT(const char*) getModuleName(void);
EXPORT(sqInt) primClosePort(void);
EXPORT(sqInt) primOpenPort(void);
EXPORT(sqInt) primRead(void);
EXPORT(sqInt) primWrite(void);
EXPORT(sqInt) setInterpreter(struct VirtualMachine *anInterpreter);


/*** Variables ***/

#if !defined(SQUEAK_BUILTIN_PLUGIN)
static sqInt (*failed)(void);
static void * (*firstIndexableField)(sqInt oop);
static sqInt (*isBytes)(sqInt oop);
static sqInt (*pop)(sqInt nItems);
static sqInt (*pushInteger)(sqInt integerValue);
static sqInt (*stSizeOf)(sqInt oop);
static sqInt (*stackValue)(sqInt offset);
static sqInt (*success)(sqInt aBoolean);
#else /* !defined(SQUEAK_BUILTIN_PLUGIN) */
extern sqInt failed(void);
extern void * firstIndexableField(sqInt oop);
extern sqInt isBytes(sqInt oop);
extern sqInt pop(sqInt nItems);
extern sqInt pushInteger(sqInt integerValue);
extern sqInt stSizeOf(sqInt oop);
extern sqInt stackValue(sqInt offset);
extern sqInt success(sqInt aBoolean);
extern
#endif
struct VirtualMachine* interpreterProxy;
static const char *moduleName =
#ifdef SQUEAK_BUILTIN_PLUGIN
	"WeDoPlugin VMMaker.oscog-eem.2480 (i)"
#else
	"WeDoPlugin VMMaker.oscog-eem.2480 (e)"
#endif
;



/*	Note: This is hardcoded so it can be run from Squeak.
	The module name is used for validating a module *after*
	it is loaded to check if it does really contain the module
	we're thinking it contains. This is important! */

	/* InterpreterPlugin>>#getModuleName */
EXPORT(const char*)
getModuleName(void)
{
	return moduleName;
}


/*	Close the WeDo port. */

	/* WeDoPlugin>>#primClosePort */
EXPORT(sqInt)
primClosePort(void)
{
	success(WeDoClosePort());
	return 0;
}


/*	Open the WeDo port. */

	/* WeDoPlugin>>#primOpenPort */
EXPORT(sqInt)
primOpenPort(void)
{
	success(WeDoOpenPort());
	return 0;
}


/*	Read data from the WeDo port into the given buffer (a ByteArray or
	String). Answer the number of bytes read.
 */

	/* WeDoPlugin>>#primRead */
EXPORT(sqInt)
primRead(void)
{
    sqInt bufOop;
    char *bufPtr;
    sqInt bufSize;
    sqInt byteCount;

	bufOop = stackValue(0);
	if (!(isBytes(bufOop))) {
		success(0);
		return 0;
	}
	bufPtr = ((char *) (firstIndexableField(bufOop)));
	bufSize = stSizeOf(bufOop);
	if (failed()) {
		return 0;
	}
	byteCount = WeDoRead(bufPtr, bufSize);
	if (byteCount < 0) {
		success(0);
		return 0;
	}
	pop(2);
	pushInteger(byteCount);
	return 0;
}


/*	Write data to the WeDo port from the given buffer (a ByteArray or String).
	Answer the number of bytes written.
 */

	/* WeDoPlugin>>#primWrite */
EXPORT(sqInt)
primWrite(void)
{
    sqInt bufOop;
    char *bufPtr;
    sqInt bufSize;
    sqInt byteCount;

	bufOop = stackValue(0);
	if (!(isBytes(bufOop))) {
		success(0);
		return 0;
	}
	bufPtr = ((char *) (firstIndexableField(bufOop)));
	bufSize = stSizeOf(bufOop);
	if (failed()) {
		return 0;
	}
	byteCount = WeDoWrite(bufPtr, bufSize);
	if (byteCount < 0) {
		success(0);
		return 0;
	}
	pop(2);
	pushInteger(byteCount);
	return 0;
}


/*	Note: This is coded so that it can be run in Squeak. */

	/* InterpreterPlugin>>#setInterpreter: */
EXPORT(sqInt)
setInterpreter(struct VirtualMachine *anInterpreter)
{
    sqInt ok;

	interpreterProxy = anInterpreter;
	ok = ((interpreterProxy->majorVersion()) == (VM_PROXY_MAJOR))
	 && ((interpreterProxy->minorVersion()) >= (VM_PROXY_MINOR));
	if (ok) {
		
#if !defined(SQUEAK_BUILTIN_PLUGIN)
		failed = interpreterProxy->failed;
		firstIndexableField = interpreterProxy->firstIndexableField;
		isBytes = interpreterProxy->isBytes;
		pop = interpreterProxy->pop;
		pushInteger = interpreterProxy->pushInteger;
		stSizeOf = interpreterProxy->stSizeOf;
		stackValue = interpreterProxy->stackValue;
		success = interpreterProxy->success;
#endif /* !defined(SQUEAK_BUILTIN_PLUGIN) */
	}
	return ok;
}


#ifdef SQUEAK_BUILTIN_PLUGIN

static char _m[] = "WeDoPlugin";
void* WeDoPlugin_exports[][3] = {
	{(void*)_m, "getModuleName", (void*)getModuleName},
	{(void*)_m, "primClosePort\000\377", (void*)primClosePort},
	{(void*)_m, "primOpenPort\000\377", (void*)primOpenPort},
	{(void*)_m, "primRead\000\000", (void*)primRead},
	{(void*)_m, "primWrite\000\000", (void*)primWrite},
	{(void*)_m, "setInterpreter", (void*)setInterpreter},
	{NULL, NULL, NULL}
};

#else /* ifdef SQ_BUILTIN_PLUGIN */

signed char primReadAccessorDepth = 0;
signed char primWriteAccessorDepth = 0;

#endif /* ifdef SQ_BUILTIN_PLUGIN */
