+static SQInteger squirrel_read_char(SQUserPointer file)
+{
+ std::istream* in = reinterpret_cast<std::istream*> (file);
+ char c = in->get();
+ if(in->eof())
+ return 0;
+ return c;
+}
+
+void compile_script(HSQUIRRELVM vm, std::istream& in, const std::string& sourcename)
+{
+ if(SQ_FAILED(sq_compile(vm, squirrel_read_char, &in, sourcename.c_str(), true)))
+ throw SquirrelError(vm, "Couldn't parse script");
+}
+
+void compile_and_run(HSQUIRRELVM vm, std::istream& in, const std::string& sourcename)
+{
+ compile_script(vm, in, sourcename);
+
+ int oldtop = sq_gettop(vm);
+
+ try {
+ sq_pushroottable(vm);
+ if(SQ_FAILED(sq_call(vm, 1, false)))
+ throw SquirrelError(vm, "Couldn't start script");
+ } catch(...) {
+ sq_settop(vm, oldtop);
+ throw;
+ }
+
+ sq_settop(vm, oldtop);
+}
+
+}