I have a Haskell RPCXML (HaXR) server process, run with GHC, that needs to execute any function that it's passed. These functions will all be defined at runtime so the compiled server won't know about them.
Is there a way to load a function definition at runtime? A method that avoids disk IO is preferable.
Thanks.
hint seems to be popular these days.
Although to load a function definition I think you will either have to put it into a module, or re-interpret it every time you use it.
Related
I'm trying to build a web app halfway between tryhaskell.org and openjscad.org. Where a user can enter Haskell code for a module that defines a solid object, evaluated server side, with the resulting object displayed in the browser.
I already have an implementation of the CSG functions required.
I'd like to be able to use mueval to safely run untrusted Haskell, much like tryhaskell.org.
I can't see how to use mueval to safely run Haskell code, including function definitions, as opposed to just evaluating Haskell statements.
I'm aware of the --loadfile argument to mueval, but this doesn't give the same security guarantees as mueval provides when evaluating expressions.
Is it possible to use mueval (or something like it) to safely evaluate untrusted Haskell modules?
I'd like to use Haskell's quickcheck library test some C code. The easiest way seems to be doing a foreign import and write a property on top of the resulting haskell function. The problem with this is that if the C code causes a segfault or manages to corrupt memory, my tests either crash without output or do something totally unpredictable.
The second alternative is to make simple executable wrappers over the C-bits and execute them outside the testing process via System.Process. Needless to say, doing this requires a lot of scaffolding and serializing values, but on the other hand, it can handle segfaults.
Is there any way of making the foreign import strategy as safe as running an external process?
You could implement the wrapper in your current process, but then use System.Posix.Process.forkProcess to run in safely in a process of its own, implementing the necessary communication using Haskell.
I am trying to get my head around compiled splices. With previouse help I can compile and render some usefull results. I don't fully understand the way it works though.
In interpreted mode, the algorithm is simple: construct root, call handler function given the mapped url, pull data from DB, construct and bind splices out of pulled data, insert them into heist and call the apropriate template.
It is all upside down in compiled mode. I map url directly to cRender and don't call a handler. So I assume all the splice constructing and data processing functions are called at load time.
So my question is when is the database called? Does this happen at load time too?
It is just the sequence of events that I don't understand.
Since splice construction is independent of a particular template rendering, does this mean the splice binding tags are unique accross the whole application?? Are they like global variables?
Thanks
Yes, you are pretty much correct. Although I wouldn't say they are like global variables. They are more like global constants, or a global API. I view compiled splices as an API that your web designer can use to interact with dynamic data.
Compiled splices allow you to insert holes into your markup that get filled with data at runtime. At load time the running monad is HeistT n IO. But at run time the running monad is RuntimeSplice n. So if you're looking at the compiled Heist API, it's very easy to see where the runtime code like database functions need to be: in the RuntimeSplice n monad.
I want to make a function called 'load' which imports definitions of functions from another file. I know how to import modules, but in my program I want the definitions of the functions to change depending on which module is 'loaded' with this new function. Is there a way to do this? Is there a better way to write my program so that this is not necessary?
I think it's type signature would look something like:
load :: String -> IO ()
where the string is the name of the module to be loaded (and the module is in the same directory).
Edit: Thanks for all the replies. Most people agree that this is not the best way to do what I want. Instead, is there a way to declare a global variable from within an I/O program. That is, I want it so that if I type (function "thing") into a function of type String -> IO(), I can still type 'thing' into GHCi to get the value assigned to it... Any suggestions?
There is almost certainly a better way to write your program so that this is not necessary. It's hard to say what without knowing more details about your situation, though. You could, for instance, represent the generic interface each module implements as a data-type, and have each module export a value of that type with the implementation.
Basically, the set of loaded modules is a static, compile-time property, so it makes no sense to want your program's behaviour to change based on its contents. Are you trying to write a library? Your users probably won't appreciate it doing such evil magic to their import lists :) (And it probably isn't possible without Template Haskell in that case, anyway.)
The exception is if you're trying to implement a Haskell tool (e.g. REPL, IDE, etc.) or trying to do plugins; i.e. dynamically-loaded modules of Haskell source code to integrate into your Haskell program. The first thing to try for those should be hint, but you may find you need something more advanced; in that case, the GHC API is probably your best bet. plugins used to be the de-facto standard in this area, but it doesn't seem to compile with GHC 7; you might want to check out direct-plugins, a simplified implementation of a similar interface that does.
mueval might be relevant; it's designed for executing short (one-line) snippets of Haskell code in a safe sandbox, as used by lambdabot.
Unless you're building a Haskell IDE or something like that, you most likely don't need this (^1).
But, in the case you do, there is always the hint-package, which allows you to embed a haskell interpreter into your program. This allows you to both load haskell modules and to convert strings into haskell values at runtime. There is a nice example of how to use it here
^1: If you're looking for a way to make things polymorphic, i.e. changing some, but not all definitions of in your code, you're probably looking for typeclasses.
With regards to your edit, perhaps you might be interested in IORef.
Google translate :
The interpreter is created, an array of bytes into the array in a machine language to cast the enum type and function, I have made an approach to dynamically execute a function, reference Please tell me if the machine-language site.
Babelfish translate:
It is to make the interpreter, but inserting machine language in arrangement at the byte unit, but if it is it makes function dynamically with the approach that it arranges the very the cast, it executes that in functional type through enum, there is a sight of the machine language which becomes reference, please teach.
Original question:
インタプリタを作っているのですが、機械語をバイト単位で配列に入れて
その配列をenumを通して関数型にキャストし、それを実行するというアプローチで関数を動的に作っているのですが、
参考になる機械語のサイトがあれば教えてください。
I'm going to make a guess - a pure guess:
You have an array of bytes containing the machine code for a function. How can you write a cast such that the function can be executed?
In which case, the answer is likely to be:
In most modern operating systems, the system protects you from converting data into executable code. The best way to deal with it would be to package the code as a function in a dynamically-loaded (shared) library, and then use the standard calls for the operating system to load that library and execute the function.