Skip to main content

How to add a new native function?

2 replies [Last post]
jayce_nqq
Offline
Joined: 2009-06-18
Points: 0

Dear all,
If I want to add a new native function in the cldc project, what I should do?

I try to add a native function in VM.java as following:

1. Add this code into VM.java after executeGC(boolean forceFullGC)

private native static void executeGC(boolean forceFullGC);
/*****the new code*****/
private native static void executeSchedule();
/**********************/

2.Add this code into Native.java

public final static int ENTRY_COUNT = 162;
/*****the new code*****/
public final static int com_sun_squawk_VM$executeSchedule = 163;
/**********************/

3. New code in bytecodes.c.spp

case Native_com_sun_squawk_VM_executeGC: {
boolean forceFullGC = popInt();
com_sun_squawk_ServiceOperation_i1 = forceFullGC;
if (TRACE) {
printStackTracePrim(-1, ip, fp, "GARBAGE_COLLECT", null);
}
threadSwitchFor(com_sun_squawk_ServiceOperation_GARBAGE_COLLECT);
break;
}
/*****the new code******/
case Native_com_sun_squawk_VM_executeSchedule: {
printf("schedule tests!!!!\n");
break;
}
/**********************/

But when I build squawk, it has the problems as followling:

root@nqq-desktop:/home/nqq/squawk/trunk# d.sh -prod -mac -o2 rom cldc
Launcher: Found tools.jar in /home/java/jdk1.5.0_19/lib/tools.jar, by popping up a level from jre.
Builder.JDK: Looking for JDK in /home/java/jdk1.5.0_19, popped up a level from jre
Launcher: Found tools.jar in /home/java/jdk1.5.0_19/lib/tools.jar, by popping up a level from jre.
For vm2c tools.jar=/home/java/jdk1.5.0_19/lib/tools.jar
[running rom...]
[running romize...]
Loaded suite stripping settings from squawk.library.properties
excluding: com.sun.squawk.compiler.*
excluding: com.sun.squawk.os.*
excluding: com.sun.squawk.BytecodeTracer
excluding: com.sun.squawk.CheneyCollector*
excluding: com.sun.squawk.Lisp2Collector*
excluding: com.sun.cldc.io.connections.HttpConnection
excluding: com.sun.squawk.ClassTunnel
[translating suite squawk [closed: false, parent: null] ...]
/home/nqq/squawk/trunk/vmcore/src/vm/rom.h is already up to date
Romizer processed 379 classes and generated these files:
/home/nqq/squawk/trunk/squawk.sym
/home/nqq/squawk/trunk/squawk.suite
/home/nqq/squawk/trunk/squawk_classes.jar
/home/nqq/squawk/trunk/squawk.suite.api
[running runvm2c...]
build failed: Could not parse macro at line 4669 in vmcore/src/vm/bytecodes.c.spp.preprocessed: Cannot use C++ style comments inside a macro

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
derek_white
Offline
Joined: 2006-09-08
Points: 0

It looks like there are two issues here:

1) The right way to add a native method is not to edit Native.java, but run the native builder tool.

See builder/nbld.sh for the correct steps. Probably need to do a clean first.

2) The error message "Cannot use C++ style comments inside a macro" is usually due to using the "//" style of comment instead of "/* */". I didn't see any "//" in your example code, but look out for that.

jayce_nqq
Offline
Joined: 2009-06-18
Points: 0

Thank you, Derek!

Now, my project works well! I delete all the new codes, and add them again, when I build the squawk, the codes work well. It's really strang, but, it's OK. Thank you for your help!