This project is read-only.

How to embed IronScheme in a Mono (Unity3D) application?

Jan 31, 2011 at 10:09 AM
Edited Feb 1, 2011 at 3:35 PM

This examples of code http://ironscheme.codeplex.com/wikipage?title=ExtensionMethods&referringTitle=Documentation (embedding IronScheme to the .NET application) works only with Microsoft .NET.

What about Mono?

P.S. If anyone interested to get my code and try it go to http://forum.unity3d.com/threads/76266-Facilities-for-script-languages-Scheme-in-particular

Jan 31, 2011 at 10:54 AM

Hi

I dont quite understand. Are you looking for a non-WPF example? If so, try http://ironscheme.codeplex.com/wikipage?title=ExtensionMethods&referringTitle=Documentation.

Cheers

leppie

Jan 31, 2011 at 10:57 AM

Yes. I did. With Mono "".Eval() can't  be compiled "the string does not have method Eval"

Jan 31, 2011 at 10:59 AM
Edited Jan 31, 2011 at 11:00 AM

Did you reference the IronScheme assembly?

Did you include the IronScheme namespace?

Does the Mono compile profile support extension methods?

Try calling IronSchemeExtensions.Eval("+") to see if the first 2 works.

PS: What version of IronScheme are you using? The binary download or the source version?

Jan 31, 2011 at 11:00 AM

"Yes" for 1 and 2, "not sure" for 3

I am a beginer with Mono yet

Jan 31, 2011 at 11:01 AM

See updated (previous) post.

Jan 31, 2011 at 11:06 AM
Edited Jan 31, 2011 at 11:10 AM

IronSchemeExtensions.Eval("(+ 1 2)");  >>> NewBehaviourScript.cs(3,3): Error CS0103: The name 'IronSchemeExtensions' does not exist in the current context (CS0103) (Test)

I am using 65218

Jan 31, 2011 at 11:11 AM

Sorry, my bad :|

It is RuntimeExtensions.Eval  :)

Jan 31, 2011 at 11:12 AM

Yes that works

Jan 31, 2011 at 11:15 AM

Good.

It appears somehow that extension methods are not working for the Mono you are using.

I suggest asking them how to enable it, or else just call RuntimeExtensions.Eval 'manually'.

Jan 31, 2011 at 11:18 AM
Edited Jan 31, 2011 at 11:24 AM

It is OK for me but only in case  if the RuntimeExtensions.Eval() have the same API features that string.Eval()

Jan 31, 2011 at 11:24 AM
Edited Jan 31, 2011 at 11:26 AM

There is another problem. When I said "it works" that mean "Mono compiler does not produce any error" But when I run the app there is exception:

SchemeException: Exception of type 'IronScheme.Runtime.SchemeException' was thrown.
#.ironscheme.exceptions..*current-exception-handlers* (object) <IL 0x0004b, 0x000f2>
#.ironscheme.exceptions..raise#1#anon#1$1666 (Microsoft.Scripting.CodeContext) <IL 0x00024, 0x0006d>
IronScheme.Runtime.Closure/ContextClosure.Call () <IL 0x0001b, 0x00039>
#.ironscheme.exceptions..dynamic-wind (object,object,object) <IL 0x00099, 0x001b6>
#.ironscheme.exceptions..with-exception-handlers (object,object) <IL 0x0006e, 0x0011e>
#.ironscheme.exceptions..raise (object) <IL 0x0006f, 0x000f6>
#.psyntax.expander..syntax-violation* (object,object,object,object) <IL 0x00261, 0x00653>
#.psyntax.expander..syntax-violation (object,object,object) <IL 0x0000a, 0x00018>
#.psyntax.expander..chi-internal (object,object,object) <IL 0x000f1, 0x002db>
#.psyntax.expander..chi-lambda-clause#1#anon#1$2034 (Microsoft.Scripting.CodeContext,object) <IL 0x00068, 0x000d9>

BTW the DLLs wich I am using I got from the latest binary distribution of IronScheme. And MonoDevelop 2.4

Jan 31, 2011 at 11:35 AM

Seems the exception message is not printed out correctly.

Will check tonite and/or fix the bug if present.

Try wrapping the call to Eval in a try/catch, then inspect the SchemeException. There is a Condition property which will contain the scheme condition that was raised.

Jan 31, 2011 at 11:38 AM

Ok. I will try.

BTW I am checking if the IronScheme can works with the Unity game engine. Wich is built with Mono and for .NET 2

Jan 31, 2011 at 11:41 AM

That's pretty cool. Let me know if you have trouble and/or success!

If successful, either you or me can write a nice blog post about it.  :)

Jan 31, 2011 at 11:48 AM

Right. It will be cool!

Jan 31, 2011 at 12:02 PM

At this point somthing works but not as it should

using System;
using UnityEngine;
using IronScheme;
using IronScheme.Runtime;

public class NewBehaviourScript : MonoBehaviour {

    void Update() {
        var r1 = RuntimeExtensions.Eval("(+ 1 2)");
        Debug.Log(r1.ToString());
    }

}

Will printout 3

 

But if i will do

RuntimeExtensions.Eval("(define x 10)");

or

RuntimeExtensions.Eval("(define (x) 10)");

The exception happen

Jan 31, 2011 at 12:28 PM
Edited Jan 31, 2011 at 12:41 PM

Approximetely same with Pure Mono (w/o Unity) and with MSVC. I tryed complie latest IronScheme from the sources. Ideal way should be

  1. Download latest source code .
  2. Open the IronScheme2008 solution (IronScheme2008.sln).
  3. Compile solution and run/debug the IronScheme.Console project.

But this does not work with Debug version. It does print allot of warnings and couple of errors

Warning    1    Parameter 'zerolast' has no matching param tag in the XML comment for 'Oyster.Math.DigitOpHelper.Shr(uint[], uint, uint, uint[], uint, int, bool)' (but other parameters do)    F:\unity\IronScheme\Oyster.IntX\OpHelpers\DigitOpHelper.cs    297    12    Oyster.IntX
Warning    2    Parameter 'zerolast' has no matching param tag in the XML comment for 'Oyster.Math.DigitOpHelper.Shr(uint*, uint, uint*, int, bool, bool)' (but other parameters do)    F:\unity\IronScheme\Oyster.IntX\OpHelpers\DigitOpHelper.cs    320    123    Oyster.IntX
Warning    3    Missing XML comment for publicly visible type or member 'Oyster.Math.IntX.operator ~(Oyster.Math.IntX)'    F:\unity\IronScheme\Oyster.IntX\IntX.cs    946    24    Oyster.IntX
Warning    4    Missing XML comment for publicly visible type or member 'Oyster.Math.IntX.BitwiseAnd(Oyster.Math.IntX, Oyster.Math.IntX)'    F:\unity\IronScheme\Oyster.IntX\IntX.cs    1033    24    Oyster.IntX
Warning    5    Missing XML comment for publicly visible type or member 'Oyster.Math.IntX.operator &(Oyster.Math.IntX, Oyster.Math.IntX)'    F:\unity\IronScheme\Oyster.IntX\IntX.cs    1038    24    Oyster.IntX

.................

292    9    IronScheme
Warning    105    'IronScheme.Runtime.Builtins.AssertionViolation(object, object, params object[])' is obsolete: 'Remove when possible'    F:\unity\IronScheme\IronScheme\Runtime\Helpers.cs    312    20    IronScheme
Warning    106    'IronScheme.Runtime.Builtins.AssertionViolation(object, object, object)' is obsolete: 'Remove when possible'    F:\unity\IronScheme\IronScheme\Runtime\Helpers.cs    323    20    IronScheme
Warning    107    'IronScheme.Runtime.Builtins.AssertionViolation(object, object, object)' is obsolete: 'Remove when possible'    F:\unity\IronScheme\IronScheme\Runtime\Helpers.cs    402    9    IronScheme
Error    108    Metadata file 'F:\unity\IronScheme\IronScheme\bin\Debug\IronScheme.dll' could not be found    IronScheme.Web.Runtime
Error    109    Metadata file 'F:\unity\IronScheme\IronScheme.Web.Runtime\bin\Debug\IronScheme.Web.Runtime.dll' could not be found    IronScheme.WebServer
Error    110    Metadata file 'F:\unity\IronScheme\IronScheme\bin\Debug\IronScheme.dll' could not be found    IronScheme.Remoting.Server
Error    111    Metadata file 'F:\unity\IronScheme\IronScheme.Web.Runtime\bin\Debug\IronScheme.Web.Runtime.dll' could not be found    IronScheme.Console
Error    112    Metadata file 'F:\unity\IronScheme\IronScheme\bin\Debug\IronScheme.dll' could not be found    IronScheme.Console
Error    113    Metadata file 'F:\unity\IronScheme\IronScheme.Remoting.Server\bin\Debug\IronScheme.Remoting.Server.dll' could not be found    IronScheme.Console
Error    114    Metadata file 'F:\unity\IronScheme\IronScheme.Web.Runtime\bin\Debug\IronScheme.Web.Runtime.dll' could not be found    IronScheme.Web
Error    115    Metadata file 'F:\unity\IronScheme\IronScheme\bin\Debug\IronScheme.dll' could not be found    IronScheme.Web
Error    116    Metadata file 'F:\unity\IronScheme\IronScheme.Console\bin\Debug\IronScheme.Console.exe' could not be found    IronScheme.Web
Error    117    Metadata file 'F:\unity\IronScheme\IronScheme\bin\Debug\IronScheme.dll' could not be found    EmbeddingConsole
Error    118    Metadata file 'F:\unity\IronScheme\IronScheme.Console\bin\Debug\IronScheme.Console.exe' could not be found    EmbeddingConsole
Error    119    Metadata file 'F:\unity\IronScheme\IronScheme\bin\Debug\IronScheme.dll' could not be found    EmbeddingSample2
Error    120    Metadata file 'F:\unity\IronScheme\IronScheme.Console\bin\Debug\IronScheme.Console.exe' could not be found    EmbeddingSample2

Jan 31, 2011 at 12:42 PM
Edited Jan 31, 2011 at 12:52 PM

Previous post about debug version. So for example for the IronScheme project there are errors in Debug target mode

Error    30    The type or namespace name 'Stopwatch' could not be found (are you missing a using directive or an assembly reference?)    F:\unity\IronScheme\IronScheme\Runtime\Ports.cs    59    13    IronScheme
Error    31    The name 'Stopwatch' does not exist in the current context    F:\unity\IronScheme\IronScheme\Runtime\Ports.cs    59    28    IronScheme
Error    32    The name 'Trace' does not exist in the current context    F:\unity\IronScheme\IronScheme\Runtime\Ports.cs    75    13    IronScheme

.......

P.S. This can be fixed by add to the Ports.cs file

#if DEBUG
using System.Diagnostics;
#endif

 

Jan 31, 2011 at 12:53 PM

It seems Unity uses a limited .NET profile like Silverlight.

While I have not spent any time getting that working, you attempt to remove or comment out the offending code, or place them within conditional compilation tags.

Example:

 

#if! UNITY
Stopwatch sw = Stopwatch.StartNew();
#endif

You should always compile IronScheme in release mode. Debug mode is really just for debugging internal bugs with IronScheme and is hardly ever needed (I can't recall when last I used it).
Jan 31, 2011 at 1:41 PM
Edited Jan 31, 2011 at 2:20 PM

works better now. I will share my code later. Now the question is. How to use 'import', 'clr-using', 'clr-call' with Unity libraries. For example my .cs script should have using UnityEngine; What should I do for scheme code?

I mean, it is not clear to me

1) which files and folders should i copy from the IronScheme folder to my target application?

2) How i can use Mono library such as UnityEngine inside the scheme code?

Right now the next code show the error.

(import
  (rnrs)
  (ironscheme clr))
(clr-reference System)
(clr-using System)

Maybe because ironscheme reads rnrs and clr form the relative path?

For now I just have copy of lib and ironscheme folders and system-libraries.ss from IronScheme.Console\bin\Release folder

Feb 1, 2011 at 5:55 AM

Hi

Sorry for only getting back to you now.

1. To use (ironscheme clr) you need to copy the ironscheme/clr.sls and ironscheme/clr/helpers.sls files to to the directory where ironscheme.dll exists. In other words, it should reflect the same directory structure. 

2. To use any assembly, you simply use (clr-reference <assembly name>). This may be tricky in some cases.

You dont have to copy system-libraries.ss, that is purely used for compilation to FASL.

If for some reason (like as in Silverlight) you cannot access the file system for Scheme libraries, you will have to embed them in IronScheme. While this has not been tested very much, you can look at the ilmerge-XXX.cmd scripts. This will create a 'monolithic' dll with everything included (or what you choose to include to cut down the size).

Cheers

leppie

 

Feb 1, 2011 at 11:02 AM
Edited Feb 1, 2011 at 11:10 AM

What about this issue?

class Console : MonoBehaviour {
  object Test() {
     return "(clr-prop-get Console showConsole {0})".Eval(this);
  }
}

Printout

> "type must be visible (Console)"

At same time the next expression

(clr-new Console)

Printount

> "You are trying to create a MonoBehaviour using the 'new' keyword.  This is not allowed.  MonoBehaviours can only be added using AddComponent().  Alternatively, your script can inherit from ScriptableObject or no base class at all"

That means scheme knows class Console as it does know it's parent class MonoBehaviour

Any Ideas?

p.s. I found the answer. class Console must be public. The error message could be more acurate

Feb 1, 2011 at 11:12 AM

Glad to see you got the answer. :)

I agree the error could be better, but to still to the CLR accessibility rules, I needed to enforce that.

I'll update the error message in the source.

Thanks

Feb 1, 2011 at 11:12 AM

Glad to see you got the answer. :)

I agree the error could be better, but to still to the CLR accessibility rules, I needed to enforce that.

I'll update the error message in the source.

Thanks

Feb 1, 2011 at 11:14 AM

:)

Next question inf the Console has a member: public bool showConsole = true;

Now this expression "(clr-prop-get Console showConsole {0})".Eval(this);

Printout

member could not be resolved on type: Console

Ideas?

Feb 1, 2011 at 11:22 AM

Sorry for the quadruple replies, work proxy is unbearably slow.

Regarding not find the member. 

You need to specify the actual class it is defined in. I dont know the API, but MonoBehaviour would be a guess if it does not have further base classes.

Feb 1, 2011 at 11:24 AM
Edited Feb 1, 2011 at 11:25 AM

This is actual class

public class Console : MonoBehaviour {
  public bool showConsole = true;
  object Test() {
     return "(clr-prop-get Console showConsole {0})".Eval(this);
  }
}

When method Test will be called it will display "member could not be resolved on type: Console"

Feb 1, 2011 at 11:30 AM

Maybe the problem is that Unity compiles my class Console to the Assembly which must be reffered by the scheme (but it is not).

At same time scheme refferes to the MonoBehaviour because script file has (clr-using UnityEngine). And scheme can fgureout that
reffered class derivered from well known MonoBehaviour but can't access to the members of Console.

Sound like scheme code should have something like (clr-using Console-Assembly)  i will try find how to reffer to.

 

Feb 1, 2011 at 11:47 AM

clr-using is similar to the construct in C#, it simply allows namespaces to be included in the type search.

To explicitly load an assembly that is not already loaded, you use clr-reference.

There could be a problem here due to the fact that the System namespace is already (by default) included. So you may have to use the entire typename, eg ConsoleNamespace.Console.

As with C# I would suggest not using the same name as types in the System namespace, particularly types from mscorlib and the System assemblies.

Feb 1, 2011 at 12:06 PM
Edited Feb 1, 2011 at 12:07 PM

Interesting. Thanks. So, what can be source of problem when system may see the class but can't access to it's public members?

(clr-is Console this)
=> True
(clr-is MonoBehaviour this)
=> True
(clr-is ConsoleBlabla this)   ;; To check if it does not print True for anything
=> type not found
(clr-prop-get Console showConsole this)
=> "member could not be resolved on type: Console"

Feb 1, 2011 at 12:16 PM
Edited Feb 1, 2011 at 12:18 PM

I have more input. Everything works if the member type for example 'int' but does not work for 'bool'. Ideas?

P.S. It make me crazy ;) Now everything works. Strange but works! :)

Feb 1, 2011 at 12:51 PM

For fields, you would use clr-field-get, not clr-prop-get  :)

Bool and Int32 maps directly to .NET, both should and must work.

Feb 1, 2011 at 1:31 PM

Thanks. Question is there somthing like a (current-directory) in the Scheme and in the C# to scheme API?

Feb 1, 2011 at 1:39 PM

Look at the (ironscheme environment) library  :)

The source will also show you how one normally interacted with .NET.

Feb 1, 2011 at 2:53 PM
Edited Feb 1, 2011 at 3:37 PM

One more stupid question ;)

I have create file fib.ss in the folder Assets

(import (rnrs))
(define (fib n)
  (if (< n 2)
      n
      (+ (fib (- n 2)) (fib (- n 1)))))
(display "Hello")

Then in the ironscheme
> (load "Assets/fib.ss")
#<unspecified>

Then
> fib
atempt to use undefined symbol

P.S. If somebody interested to get my code and try it go to http://forum.unity3d.com/threads/76266-Facilities-for-script-languages-Scheme-in-particular

 

Feb 1, 2011 at 4:58 PM

If you simply want to evaluate the content into the interaction-environment, you would use (include "filename"), not (load "filename").

Feb 1, 2011 at 5:01 PM

Great! Thanks.

Feb 1, 2011 at 6:26 PM

I just made a little blog post. See http://xacc.wordpress.com/2011/02/01/using-ironscheme-in-unity3d/.

Thanks for your efforts.

Feb 2, 2011 at 12:51 PM
Edited Feb 2, 2011 at 12:52 PM

Ok. Great

In published by unity application the IronScheme does not work. There is the LogFile below. Any ides? Also Interesting can be IronScheme builded with Mono?

Mono path[1] = 'C:/Users/valery/Desktop/Scheme_Data/Mono'
Direct3D:
    Version:  Direct3D 9.0c [nvd3dum.dll 8.17.12.6658]
    Renderer: NVIDIA GeForce 7600 GT
    Vendor:   NVIDIA
    VRAM:     256 MB
    Caps:     Shader=30 DepthRT=1 NativeDepth=0 NativeShadow=1 DF16=0 DF24=0 INTZ=0 RAWZ=1 NULL=1 RESZ=0 SlowINTZ=0
desktop: 1440x900 60Hz; virtual: 1440x900 at 0,0
<I> Initializing (RawInput).

<RI> Input initialized.

Non platform assembly: data-0253D898 (this message is harmless)
Non platform assembly: data-025A1108 (this message is harmless)
Non platform assembly: data-025A3778 (this message is harmless)
Non platform assembly: data-06AF0048 (this message is harmless)
Non platform assembly: data-06B3E6B0 (this message is harmless)
Non platform assembly: data-025B1FC8 (this message is harmless)
Non platform assembly: data-06E40020 (this message is harmless)
Platform assembly: C:\Users\valery\Desktop\Scheme_Data\Managed\System.dll (this message is harmless)
MissingMethodException: Method not found: 'System.Console.get_KeyAvailable'.
  at Microsoft.Scripting.Shell.CommandLine..cctor () [0x00000] in <filename unknown>:0
Rethrow as TypeInitializationException: An exception was thrown by the type initializer for Microsoft.Scripting.Shell.CommandLine
  at IronScheme.Hosting.IronSchemeLanguageProvider+CommandLineX..ctor () [0x00000] in <filename unknown>:0
  at IronScheme.Hosting.IronSchemeLanguageProvider..ctor (Microsoft.Scripting.ScriptDomainManager x) [0x00000] in <filename unknown>:0
  at (wrapper managed-to-native) System.Reflection.MonoCMethod:InternalInvoke (object,object[],System.Exception&)
  at System.Reflection.MonoCMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00000] in <filename unknown>:0
Rethrow as InvalidImplementationException: Type 'IronScheme.Hosting.IronSchemeLanguageProvider' doesn't provide a suitable public constructor or its implementation is faulty.
  at Microsoft.Scripting.Utils.ReflectionUtils.CreateInstance[LanguageProvider] (System.Type actualType, System.Object[] args) [0x00000] in <filename unknown>:0
  at Microsoft.Scripting.ScriptDomainManager+LanguageProviderDesc.LoadProvider (Microsoft.Scripting.ScriptDomainManager manager) [0x00000] in <filename unknown>:0
  at Microsoft.Scripting.ScriptDomainManager.GetLanguageProvider (System.Type type) [0x00000] in <filename unknown>:0
  at IronScheme.RuntimeExtensions..cctor () [0x00000] in <filename unknown>:0
Rethrow as TypeInitializationException: An exception was thrown by the type initializer for IronScheme.RuntimeExtensions
  at ScmConsole.Awake () [0x00000] in <filename unknown>:0

(Filename:  Line: -1)

Feb 2, 2011 at 12:56 PM

This is what I meant with the Silverlight profile. It does not have certain parts to it, eg the System.Console.

I will see if I can try remove some Silverlight parts that is know to cause problems, or workaround it issue, by issue.

Is there a 'free' version of Unity3D that I can test it with? I doubt 30 days trial will be sufficient for testing, given I am not going to do it 30 days full time (work, weekends, etc).

Feb 2, 2011 at 1:00 PM
Edited Feb 2, 2011 at 1:19 PM

Yes. Unity is free. It has trial period only for Pro version. I do not use any Pro features anyway. Just download and install unity then activate it with normal (official) unity way as Indie version. (So do not select Pro/iOS). Even if you will register Pro after Trial period you can swich to Indie.

Thanks for Help

 

P.S. But everything works inside Unity Editor. So problem only for builded app.