Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Tagged as

ObjectScript: A new programming language

, 9 Oct 2012 MIT
The ObjectScript is a new programing language that mixes benefits of JavaScript, Lua, and PHP. The ObjectScript has syntax from JavaScript, multiple results from Lua, OOP from PHP and much more.
OS-0.98-rc-noexe.ZIP
OS
examples-os
add_user_module.os
bind.os
core.os
run_os_prog.os
test.os
proj.win32
add_user_module
add_user_module.vcxproj.filters
os
os.vcxproj.filters
osbind
osbind.vcxproj.filters
profile_benchmark
profile_benchmark.vcxproj.filters
scripts
test_fannkuch.lua
test_fannkuch.os
run_os_prog
run_os_prog.vcxproj.filters
stack_usage
stack_usage.vcxproj.filters
README.md
source
os-binder-FunctionClassImp.tpl
os-binder-FunctionImp.tpl
os-binder-FunctionImpVoid.tpl
OS-0.98-rc.ZIP
bin
os.exe
add_user_module.os
bind.os
core.os
run_os_prog.os
test.os
Makefile
add_user_module.vcxproj.filters
os.vcxproj.filters
osbind.vcxproj.filters
profile_benchmark.vcxproj.filters
test_fannkuch.lua
test_fannkuch.os
run_os_prog.vcxproj.filters
stack_usage.vcxproj.filters
README.md
os-binder-FunctionClassImp.tpl
os-binder-FunctionImp.tpl
os-binder-FunctionImpVoid.tpl
unitpoint-objectscript-86f30d7-noexe.zip
unitpoint-objectscript-86f30d7
.gitignore
README.md
bin
.gitignore
examples-os
add_user_module.os
bind.os
core.os
run_os_prog.os
test.os
proj.win32
.gitignore
add_user_module
.gitignore
os
.gitignore
test_fannkuch.osc
test_fannkuch.osd
osbind
.gitignore
profile_benchmark
.gitignore
scripts
test_fannkuch.lua
test_fannkuch.os
run_os_prog
.gitignore
stack_usage
.gitignore
source
.gitignore
os-binder-FunctionClassImp.tpl
os-binder-FunctionImp.tpl
os-binder-FunctionImpVoid.tpl
unitpoint-objectscript-86f30d7.zip
.gitignore
Makefile
README.md
.gitignore
os.exe
add_user_module.os
bind.os
core.os
run_os_prog.os
test.os
.gitignore
.gitignore
.gitignore
test_fannkuch.osc
test_fannkuch.osd
.gitignore
.gitignore
test_fannkuch.lua
test_fannkuch.os
.gitignore
.gitignore
.gitignore
os-binder-FunctionClassImp.tpl
os-binder-FunctionImp.tpl
os-binder-FunctionImpVoid.tpl
#include "stdafx.h"
#include "../../source/objectscript.h"
#include "../../source/os-binder.h"
#include <ctype.h>
#include <direct.h>

using namespace ObjectScript;

std::string getcwdString()
{
	const int PATH_MAX = 1024;
	char buf[PATH_MAX];
	getcwd(buf, PATH_MAX);
	return buf;
}

bool my_isdigit(const OS::String& str)
{
	int len = str.getLen();
	for(int i = 0; i < len; i++){
		if(!isdigit(str[i])){
			return false;
		}
	}
	return len > 0;
}

std::string my_hash(const char * str)
{
	int i, len = strlen(str), hash = 5381;
	for(i = 0; i < len; i++){
		hash = ((hash << 5) + hash) + str[i];
	}
	hash &= 0x7fffffff;
	char buf[16];
	for(i = 0; hash > 0; hash >>= 4){
		buf[i++] = "0123456789abcdef"[hash & 0xf];
	}
	buf[i] = 0;
	return buf;
}

void my_print_num(int i)
{
	printf("my_print_num: %d\n", i);
}

void my_print_void(void)
{
	printf("my_print_void\n");
}

long double my_fabs(long double a)
{
	return a >= 0 ? a : -a;
}

class TestClass
{
public:
	int i;
	float j;

	TestClass(int _i, float _j)
	{ 
		i = _i; j = _j;
	}
	~TestClass()
	{
		// you could set breakpoint here to be sure that this object is deleted automaticaly
	}

	int getI() const { return i; }
	void setI(int _i){ i = _i; }
	
	float getJ() const { return j; }
	void setJ(float _j){ j = _j; }

	TestClass * clone()
	{
		return new TestClass(*this);
	}

	TestClass * add(TestClass * a, TestClass * b)
	{
		return new TestClass(a->i + b->i, a->j + b->j);
	}

	double doSomething(int a, float b, double c, TestClass * pb)
	{
		return i + j + a + b + c + pb->i + pb->j;
	}

	void print()
	{
		printf("test class: %d, %f\n", i, j);
	}
};

namespace ObjectScript { OS_DECL_USER_CLASS(TestClass); }

TestClass * __constructTestClass(int i, float j)
{
	return new TestClass(i, j);
}

void registerTestClass(OS * os)
{
	OS::FuncDef funcs[] = {
		def("__construct", __constructTestClass),
		def("__get@i", &TestClass::getI),
		def("__set@i", &TestClass::setI),
		def("__get@j", &TestClass::getJ),
		def("__set@j", &TestClass::setJ),
		def("__clone", &TestClass::clone),
		def("__add", &TestClass::add),
		def("doSomething", &TestClass::doSomething),
		def("print", &TestClass::print),
		{}
	};
	registerUserClass<TestClass>(os, funcs);
}

void initMyModule(OS * os)
{
	OS::FuncDef funcs[] = {
		def("isdigit", my_isdigit),
		def("hash", my_hash),
		def("print_num", my_print_num),
		def("print_void", my_print_void),
		def("abs", my_fabs),
		def("fabs", (double(__cdecl*)(double))fabs),
		{}
	};
	os->getModule("my");
	os->setFuncs(funcs);
	os->pop();
}

struct TestStruct
{
	float a, b;

	TestStruct(){ a = b = 0; }
	TestStruct(float _a, float _b){ a = _a; b = _b; }
};

void printTestStruct(const TestStruct& p)
{
	printf("TestStruct: %f %f\n", p.a, p.b);
}

TestStruct changeTestStruct(const TestStruct& p)
{
	return TestStruct(p.a*10, p.b*100);
}

namespace ObjectScript {

OS_DECL_USER_CLASS(TestStruct);

template <>
struct CtypeValue<TestStruct>
{
	typedef TestStruct type;

	static bool isValid(const TestStruct&){ return true; }

	static TestStruct def(ObjectScript::OS * os){ return TestStruct(0, 0); }
	static TestStruct getArg(ObjectScript::OS * os, int offs)
	{
		if(os->isObject(offs)){
			os->getProperty(offs, "a"); // required
			float a = os->popFloat();
		
			os->getProperty(offs, "b"); // required
			float b = os->popFloat();

			return TestStruct(a, b);
		}
		os->triggerError(OS_E_ERROR, "TestStruct expected");
		return TestStruct(0, 0);
	}

	static void push(ObjectScript::OS * os, const TestStruct& p)
	{
		os->newObject();
	
		os->pushStackValue();
		os->pushNumber(p.a);
		os->setProperty("a", false, false);
				
		os->pushStackValue();
		os->pushNumber(p.b);
		os->setProperty("b", false, false);
	}
};

} // namespace ObjectScript

int _tmain(int argc, _TCHAR* argv[])
{
	// craete ObjectScript instance
	OS * os = OS::create();

	registerTestClass(os);
	initMyModule(os);

	os->setGlobal(def("getcwd", getcwdString));
	os->setGlobal(def("printTestStruct", printTestStruct));
	os->setGlobal(def("changeTestStruct", changeTestStruct));

	// run program
	os->require("../../examples-os/bind.os");

	// release the ObjectScript instance
	os->release();
	return 0;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The MIT License

Share

About the Author

unitpoint

United States United States
No Biography provided

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.1411023.1 | Last Updated 9 Oct 2012
Article Copyright 2012 by unitpoint
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid