Monkey & Third party modules

Monkey Forums/Monkey Programming/Monkey & Third party modules

Armitage1982(Posted 2012) [#1]
Following my last entries, it's been a while since I own Monkey but didn't use it because of the lack of functionality.

The language looks awesome but if I want to use it the same way I'm using BlitzMax, I need a few more thirds parties modules.

Is there any changes or possibilities for using external C++ Libraries in the current Monkey?

I mean, is it still harder than BlitzMax to do this if you stick to desktop programming? I'm already having some difficulties to wrap modules under BlitzMax so I was wondering if new solutions didn't appears were BRL development is.


ziggy(Posted 2012) [#2]
I have not tried it myself on C++, but .net is almost "automatic" to import, so I supose it will be more or less the same for C++, except for the garbage collecting wich may need some glue code. anyway, bear in mind that the current c++ implementation laks a real garbage collector and it is only collecting garbage when used with Mojo, and when the program is running in the mojo event-based system, so not exactly very "safe" to use this target other than in conjunction with Mojo.
Mark did say he was going to implement a decent Garbage Collector on the C++ target "at some point". I don't know how soon or late this is, but until this happens, you won't be able to free memory from dead objects.


Armitage1982(Posted 2012) [#3]
So how exactly is it working for .Net ?
I mean you import your library externally with Monkey and then you can use it straight away?
I thought you still need to glue every Class / Methods convert the objects nope ?


ziggy(Posted 2012) [#4]
It's ultra easy. Example:
Import mojo


Extern

Class Collection = "@System.Collections.Generic.Dictionary<String,String>"
	Method Add(key:String, value:String)
	Method ContainsKey:Bool(key:String)
End

Public

Function Main:Int()
	Local col:Collection = new Collection
	col.Add("Hello","World!")
	if col.ContainsKey("Hello") 
		Print "Contains Hello"
	Else
		Print "Does not contain Hello"
	EndIf
	Return 0
End




I'm integrating a .Net <String,String> dictioary into Monkey and exposing its "Add" method, and its "Contains" method. No extra code needed. (Be sure to be selecting the XNA target for this sample to work).


Armitage1982(Posted 2012) [#5]
...Wow It's incredibly easy compared to BlitzMax!

I wonder if it's working the same way for C++ API?
Even if we have to link a few things to a garbage collector or going with memory leaks until a nice GC implementation, it's worth everything !!

I'm surprise such thing is not more highlighted and that we don't see more topic about using celeb libraries with Monkey. Is it something new !?


ziggy(Posted 2012) [#6]
I think it's more or less the same, but I'm not sure how Garbage Collector has to be set up in this scenario.
This is a small sample that demonstrates the usage of C++ elements on Monkey. It's very simple:


file stdio.monkey:
#rem
	header: This module is a very simple I/O layer for the stdcpp target.
	It provides the basic functionality to send string data to the Standard Output Pipe, and to the Standard Error Pipe. It also provides an Input function to get data from the user, using the typical console prompt.
#end

'We don't want the reflection module to break everythinc C based here, so, we add a filter:
'#REFLECTION_FILTER="stdio"  

#if TARGET="stdcpp" Or TARGET="glfw"
	'Private
	Extern private
	
	Class FILE
	End
	
	Global stdin:FILE
	Global stdout:FILE
	Global stderr:FILE
	
	Function fputc(c,file:FILE)
	Function fflush(file:FILE)
	Function fgetc:Int(file:FILE)
	'Const EOF
	Public
	
	'summary: Prompt for user input in the system console window and resturns a String
	Function Input:String(prompt:String=">")
		
		Local c:Int, result:String
		
		For Local i:Int = 0 until prompt.Length 
			fputc(prompt[i],stdout)
		end
		fflush(stdout)
		
		c=fgetc(stdin)
		
		While c<>10 And c<>13
			result += String.FromChar(c)
			c=fgetc(stdin);
		Wend
		
		fflush(stdin)
		
		Return result;
	End
	
	'summary: Sends text to the standard output pipe
	Function Output:void(value:String)
		For Local i:Int = 0 to value.Length-1
			fputc(value[i],stdout)
		Next
		fflush(stdout)
	End
	
	'summary: Sends text to the standard error pipe
	Function ErrOutput:void(value:String)
		For Local i:Int = 0 to value.Length-1
			fputc(value[i],stderr )
		Next
		fflush(stderr)
	End
#Else
	#Error "The Jungle stdio module is only supported in C++ based targets such as stdcpp or glfw."
#End 

#rem
	footer: 
Copyright (c) 2011-2012 Manel Ibáñez Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#end


This module has to keep outside the reflection filter. I'm not sure why tho.


fsoft(Posted 2012) [#7]
Hi ziggy,

could you please tell me why are you using fputc() instead of fprintf()?
Aren't Monkey string zero terminated?


Armitage1982(Posted 2012) [#8]
It would be nice to get a little more documentation or a tutorial inside Monkey about this.
Because I guess you have to add library include and import somewhere to successfully link everything.


ziggy(Posted 2012) [#9]
could you please tell me why are you using fputc() instead of fprintf()?

Aren't Monkey string zero terminated?

No, AFAIK they're not string zero terminated.
Example (compile on stdcpp):
Function Main()
	Local message:String = "Hello~0 wolrd!"
	Print message
	Print message.Length()
End

You can have the zero char on a string, so they're not zero termianted, wich is great. String zero terminated strings are usual on 8 bits encodings. I'm not sure if monkey does support Unicode in all its targets, but it does in some, it had no sense to make only some of them support the zero character in a String content.


Armitage1982(Posted 2012) [#10]
Is it possible to implement callbacks when using an external XNA libs ?


AdamRedwoods(Posted 2012) [#11]
Is it possible to implement callbacks when using an external XNA libs ?

if you need function pointers, no. I can make function pointers happen in a modified target like wxMonkey, but not standard.

I haven't tried Extern Interfaces, though. That would be another possibility to try.


Gerry Quinn(Posted 2012) [#12]
Has anyone tried to make Monkey interface with DLLs?


ziggy(Posted 2012) [#13]
Is it possible to implement callbacks when using an external XNA libs ?
Yes, C# support function pointers, but you may need to write this glue code in C# and extern it on Monkey. Anyway, the appropriate way to do callbacks on C# would be to use delegates which I've been requesting for a long time now, but I think I'm the only one asking for them, so it looks like something that won't happen anytime soon.