示例#1
0
utString utStringFormat(const char *format, ...)
{
#ifdef _MSC_VER
# define ut_vsnprintf    _vsnprintf_s
#else
# define ut_vsnprintf    vsnprintf
#endif

    va_list args;
    va_start(args, format);

    int       nBuf     = 0;
    const int BUF_SIZE = 1024;
    char      szBuffer[BUF_SIZE + 1];

    nBuf = ut_vsnprintf(szBuffer, BUF_SIZE, format, args);

    if (nBuf < 0)
    {
        szBuffer[BUF_SIZE] = 0;
    }

    va_end(args);

    return utString(szBuffer);

#undef ut_vsnprintf
}
示例#2
0
static bool readDefaultFontFaceBytes(void** mem, size_t* size)
{
#if LOOM_PLATFORM == LOOM_PLATFORM_WIN32
	// Get Windows dir
	char windir[MAX_PATH];
	GetWindowsDirectoryA((LPSTR)&windir, MAX_PATH);

	// Load font file
	return readFontFile((utString(windir) + "\\Fonts\\arial.ttf").c_str(), mem, size) != 0;

	// Kept for future implementation of grabbing fonts by name
	/*
	SDL_SysWMinfo info;
	SDL_VERSION(&info.version);
	if (SDL_GetWindowWMInfo(gSDLWindow, &info)) {
	HWND windowHandle = info.info.win.window;
	HDC deviceContext = GetDC(windowHandle);
	DWORD size = GetFontData(deviceContext, 0, 0, NULL, 0);
	lmAssert(size != GDI_ERROR, "Font data retrieval failed: %d", GetLastError());
	}
	else {
	lmLogError(gGFXVectorRendererLogGroup, "Error retrieving window information: %s", SDL_GetError());
	}
	*/
#elif LOOM_PLATFORM == LOOM_PLATFORM_ANDROID
	return readFontFile("/system/fonts/DroidSans.ttf", mem, size) != 0;
#elif LOOM_PLATFORM == LOOM_PLATFORM_OSX
	return readFontFile("/Library/Fonts/Arial.ttf", mem, size) != 0;
#elif LOOM_PLATFORM == LOOM_PLATFORM_IOS
    return (bool)platform_fontSystemFontFromName("ArialMT", mem, (unsigned int*)size);
#else
	mem = NULL;
	size = 0;
#endif
}
示例#3
0
文件: main.cpp 项目: Y-way/LoomSDK
static void enqueueFileChangeCallback(const char *path)
{
    CallbackQueueNote *cqn = lmNew(NULL) CallbackQueueNote();

    cqn->type = QNT_Change;
    cqn->text = utString(path);

    loom_mutex_lock(gCallbackLock);
    gCallbackQueue.push_back(cqn);
    loom_mutex_unlock(gCallbackLock);
}
示例#4
0
文件: main.cpp 项目: Y-way/LoomSDK
static void enqueueLogCallback(const char *msg)
{
    CallbackQueueNote *cqn = lmNew(NULL) CallbackQueueNote();

    cqn->type = QNT_Log;
    cqn->text = utString(msg);

    loom_mutex_lock(gCallbackLock);
    gCallbackQueue.push_back(cqn);
    loom_mutex_unlock(gCallbackLock);
}
示例#5
0
void LSCompiler::linkRootAssembly(const utString& sjson)
{
    json_error_t jerror;
    json_t       *json = json_loadb((const char *)sjson.c_str(), sjson.length(), 0, &jerror);

    lmAssert(json, "Error linking assembly");

    json_t *ref_array = json_object_get(json, "references");
    lmAssert(json, "Error linking assembly, can't get executable references");

    for (UTsize i = 0; i < rootBuildDependencies.size(); i++)
    {
        BuildInfo *buildInfo     = rootBuildDependencies.at(i);
        utString  assemblySource = buildInfo->getOutputDir() + platform_getFolderDelimiter() + buildInfo->getAssemblyName() + ".loomlib";

        utArray<unsigned char> rarray;
        lmAssert(utFileStream::tryReadToArray(assemblySource, rarray), "Unable to load library assembly %s", assemblySource.c_str());

        utBase64 base64 = utBase64::encode64(rarray);

        for (size_t j = 0; j < json_array_size(ref_array); j++)
        {
            json_t   *jref = json_array_get(ref_array, j);
            utString jname = json_string_value(json_object_get(jref, "name"));
            if (buildInfo->getAssemblyName() == jname)
            {
                logVerbose("Linking: %s", jname.c_str());

                json_object_set(jref, "binary", json_string(base64.getBase64().c_str()));
                json_object_set(jref, "uid", json_string(readAssemblyUID(rarray)));
                break;
            }
        }
    }

    // filter the reference array by the import assemblies
    utStack<int> filter;
    for (size_t j = 0; j < json_array_size(ref_array); j++)
    {
        json_t   *jref = json_array_get(ref_array, j);
        utString jname = json_string_value(json_object_get(jref, "name"));

        bool found = false;

        // always find the System assembly, so we don't have to explicitly import from it
        if (jname == "System")
        {
            found = true;
        }

        for (UTsize k = 0; k < importedAssemblies.size() && !found; k++)
        {
            if (importedAssemblies.at(k)->getName() == jname)
            {
                found = true;
                break;
            }
        }

        if (!found)
        {
            filter.push((int)j);
        }
    }

    while (filter.size())
    {
        json_array_remove(ref_array, filter.pop());
    }

    for (UTsize i = 0; i < rootLibDependencies.size(); i++)
    {
        utString libName = rootLibDependencies.at(i);

        for (size_t j = 0; j < json_array_size(ref_array); j++)
        {
            json_t   *jref = json_array_get(ref_array, j);
            utString jname = json_string_value(json_object_get(jref, "name"));

            if (libName != jname)
            {
                continue;
            }

            log("Linking: %s", libName.c_str());

            utString delim   = platform_getFolderDelimiter();
            utString libPath = sdkPath + delim + "libs" + delim + libName + ".loomlib";

            utArray<unsigned char> rarray;

            if (libName == "System" && embeddedSystemAssembly)
            {
                size_t embeddedSystemAssemblyLength = strlen(embeddedSystemAssembly);
                rarray.resize((int)(embeddedSystemAssemblyLength + 1));
                memcpy(&rarray[0], embeddedSystemAssembly, embeddedSystemAssemblyLength + 1);
            }
            else
            {
                lmAssert(utFileStream::tryReadToArray(libPath, rarray), "Unable to load library assembly %s at %s", libName.c_str(), libPath.c_str());    
            }

            utBase64 base64 = utBase64::encode64(rarray);
            json_object_set(jref, "binary", json_string(base64.getBase64().c_str()));
            json_object_set(jref, "uid", json_string(readAssemblyUID(rarray)));

            break;
        }
    }

    json_object_set(json, "executable", json_true());

    utString execSource = rootBuildInfo->getOutputDir() + utString(platform_getFolderDelimiter()) + rootBuildInfo->getAssemblyName() + ".loom";

    // generate binary assembly for executable
    BinWriter::writeExecutable(execSource.c_str(), json);

    log("Compile Successful: %s\n", execSource.c_str());
}
示例#6
0
utString operator+(const utString& lhs, const utString& rhs)
{
    return utString(lhs) += rhs;
}
示例#7
0
void BinWriter::writeClass(json_t *jclass)
{
    writeMemberInfo(jclass);

    // handle class modifiers

    json_t *attr_array = json_object_get(jclass, "classattributes");

    bytes.writeInt((int)json_array_size(attr_array));

    for (size_t i = 0; i < json_array_size(attr_array); i++)
    {
        bytes.writeInt((int)poolJString(json_array_get(attr_array, i)));
    }

    // base class
    int ibaseType = -1;

    utString sbaseType = json_string_value(
        json_object_get(jclass, "baseType"));

    if (sbaseType.size() > 0)
    {
        ibaseType = poolString(sbaseType.c_str());
    }

    bytes.writeInt(ibaseType);

    // interfaces
    json_t *jinterfaces = json_object_get(jclass, "interfaces");

    bytes.writeInt((int)json_array_size(jinterfaces));

    for (size_t i = 0; i < json_array_size(jinterfaces); i++)
    {
        json_t *o = json_array_get(jinterfaces, i);
        bytes.writeInt(poolString(json_string_value(o)));
    }

    // delegate types
    json_t *jdelegate_types = json_object_get(jclass, "delegateTypes");

    bytes.writeInt((int)json_array_size(jdelegate_types));

    for (size_t i = 0; i < json_array_size(jdelegate_types); i++)
    {
        json_t *o = json_array_get(jdelegate_types, i);
        bytes.writeInt(poolString(json_string_value(o)));
    }


    // delegate return type
    int idelegatereturntype = -1;

    utString sdelegateReturnType = json_string_value(
        json_object_get(jclass, "delegateReturnType"));

    if (sdelegateReturnType.size() > 0)
    {
        idelegatereturntype = poolString(sdelegateReturnType.c_str());
    }

    bytes.writeInt(idelegatereturntype);

    // write imports

    json_t *import_array = json_object_get(jclass, "imports");

    bytes.writeInt((int)json_array_size(import_array));

    for (size_t i = 0; i < json_array_size(import_array); i++)
    {
        json_t *jimport = json_array_get(import_array, i);
        bytes.writeInt(poolString(json_string_value(jimport)));
    }

    // write constructor
    json_t *jconstructor = json_object_get(jclass, "constructor");

    if (jconstructor)
    {
        bytes.writeBoolean(true);
        writeConstructor(jconstructor);
    }
    else
    {
        bytes.writeBoolean(false);
    }

    // write fields

    json_t *field_array = json_object_get(jclass, "fields");

    bytes.writeInt((int)json_array_size(field_array));

    for (size_t i = 0; i < json_array_size(field_array); i++)
    {
        json_t *fo = json_array_get(field_array, i);
        writeField(fo);
    }

    // write properties
    json_t *prop_array = json_object_get(jclass, "properties");

    bytes.writeInt((int)json_array_size(prop_array));

    for (size_t i = 0; i < json_array_size(prop_array); i++)
    {
        json_t *po = json_array_get(prop_array, i);
        writeProperty(po);
    }


    // write methods
    json_t *method_array = json_object_get(jclass, "methods");

    bytes.writeInt((int)json_array_size(method_array));

    for (size_t i = 0; i < json_array_size(method_array); i++)
    {
        json_t *jmethod = json_array_get(method_array, i);
        writeMethodInfo(jmethod);
    }

    // static initializer byte code
    ByteCode byteCode;


    byteCode.setBase64   (utString(json_string_value(json_object_get(jclass, "bytecode_staticinitializer"))));
#if LOOM_ENABLE_JIT
    byteCode.setBase64FR2(utString(json_string_value(json_object_get(jclass, "bytecode_staticinitializer_fr2"))));
#endif
    byteCode.serialize(&bytes);


    byteCode.setBase64   (utString(json_string_value(json_object_get(jclass, "bytecode_instanceinitializer"))));
#if LOOM_ENABLE_JIT
    byteCode.setBase64FR2(utString(json_string_value(json_object_get(jclass, "bytecode_instanceinitializer_fr2"))));
#endif
    byteCode.serialize(&bytes);

}
示例#8
0
void BinWriter::writeMethodBase(json_t *jmbase)
{
    writeMemberInfo(jmbase);

    // handle modifiers
    json_t *mod_array = json_object_get(jmbase, "methodattributes");

    bytes.writeInt((int)json_array_size(mod_array));

    for (size_t i = 0; i < json_array_size(mod_array); i++)
    {
        bytes.writeInt(poolJString(json_array_get(mod_array, i)));
    }

    // template types on return
    json_t *ttypes = json_object_get(jmbase, "templatetypes");
    if (ttypes && json_is_object(ttypes))
    {
        bytes.writeBoolean(true);
        writeTemplateTypes(ttypes);
    }
    else
    {
        bytes.writeBoolean(false);
    }

    // parameters
    json_t *parameter_array = json_object_get(jmbase, "parameters");

    bytes.writeInt((int)json_array_size(parameter_array));

    for (size_t i = 0; i < json_array_size(parameter_array); i++)
    {
        json_t *p = json_array_get(parameter_array, i);

        bytes.writeInt(poolJString(json_object_get(p, "name")));
        utString stype = json_string_value(json_object_get(p, "type"));
        if (stype.size() > 0)
        {
            bytes.writeBoolean(true);
            bytes.writeInt(poolString(stype.c_str()));
        }
        else
        {
            bytes.writeBoolean(false);
        }

        if (json_is_true(json_object_get(p, "hasdefault")))
        {
            bytes.writeBoolean(true);
        }
        else
        {
            bytes.writeBoolean(false);
        }

        if (json_is_true(json_object_get(p, "isvarargs")))
        {
            bytes.writeBoolean(true);
        }
        else
        {
            bytes.writeBoolean(false);
        }

        // handle template types
        json_t *ttypes = json_object_get(p, "templatetypes");

        bytes.writeInt((int)json_array_size(ttypes));

        for (size_t i = 0; i < json_array_size(ttypes); i++)
        {
            bytes.writeInt(poolJString(json_array_get(ttypes, i)));
        }
    }

    ByteCode byteCode;

    byteCode.setBase64(utString(json_string_value(json_object_get(jmbase, "bytecode"))));
#if LOOM_ENABLE_JIT
    byteCode.setBase64FR2(utString(json_string_value(json_object_get(jmbase, "bytecode_fr2"))));
#endif
    byteCode.serialize(&bytes);

}
示例#9
0
void TypeReader::deserializeClass(Type *type, json_t *classJSON)
{
    utString sbaseType = json_string_value(
        json_object_get(classJSON, "baseType"));

    if (sbaseType.size() > 0)
    {
        Type *baseType =
            type->getModule()->getAssembly()->getLuaState()->getType(
                sbaseType.c_str());
        lmAssert(baseType != NULL, "Unable to resolve type '%s' referenced as base of type '%s'",
                 sbaseType.c_str(), type->getFullName().c_str());
        type->setBaseType(baseType);
    }

    json_t *jinterfaces = json_object_get(classJSON, "interfaces");

    for (size_t i = 0; i < json_array_size(jinterfaces); i++)
    {
        json_t   *o     = json_array_get(jinterfaces, i);
        utString sface  = json_string_value(o);
        Type     *itype = type->getModule()->getAssembly()->getLuaState()->getType(
            sface.c_str());
        assert(itype);
        type->addInterface(itype);
    }

    json_t *jdelegateTypes = json_object_get(classJSON, "delegateTypes");

    for (size_t i = 0; i < json_array_size(jdelegateTypes); i++)
    {
        json_t   *o     = json_array_get(jdelegateTypes, i);
        utString stype  = json_string_value(o);
        Type     *itype = type->getModule()->getAssembly()->getLuaState()->getType(
            stype.c_str());
        assert(itype);
        type->addDelegateType(itype);
    }

    utString sdelegateReturnType = json_string_value(
        json_object_get(classJSON, "delegateReturnType"));

    if (sdelegateReturnType.size() > 0)
    {
        Type *delegateReturnType =
            type->getModule()->getAssembly()->getLuaState()->getType(
                sdelegateReturnType.c_str());
        assert(delegateReturnType);
        type->setDelegateReturnType(delegateReturnType);
    }

    // meta data

    MemberInfoReader::deserializeMetaInfo(type, json_object_get(classJSON, "metainfo"));

    // handle imports
    json_t *iarray = json_object_get(classJSON, "imports");
    for (size_t i = 0; i < json_array_size(iarray); i++)
    {
        json_t   *jimport = json_array_get(iarray, i);
        utString import   = json_string_value(jimport);

        Type *timport =
            type->getModule()->getAssembly()->getLuaState()->getType(
                import.c_str());
        type->addImport(timport);
    }

    json_t *jconstructor = json_object_get(classJSON, "constructor");
    if (jconstructor)
    {
        MethodBase *m = NULL;
        m = MethodReader::deserializeConstructorInfo(type, jconstructor);
        type->addMember(m);
    }

    // handle fields
    json_t *farray = json_object_get(classJSON, "fields");
    for (size_t i = 0; i < json_array_size(farray); i++)
    {
        json_t    *fo = json_array_get(farray, i);
        FieldInfo *f  = FieldInfoReader::deserializeFieldInfo(type, fo);
        type->addMember(f);
    }

    // handle properties

    json_t *parray = json_object_get(classJSON, "properties");
    for (size_t i = 0; i < json_array_size(parray); i++)
    {
        json_t       *po = json_array_get(parray, i);
        PropertyInfo *p  = PropertyInfoReader::deserializePropertyInfo(type, po);
        type->addMember(p);
    }

    // handle methods
    farray = json_object_get(classJSON, "methods");
    for (size_t i = 0; i < json_array_size(farray); i++)
    {
        json_t *fo = json_array_get(farray, i);

        MethodBase *m = NULL;
        m = MethodReader::deserializeMethodInfo(type, fo);
        type->addMember(m);
    }

    ByteCode *byteCode;

    byteCode = lmNew(NULL) ByteCode();
    byteCode->setBase64(utString(json_string_value(json_object_get(classJSON, "bytecode_staticinitializer"))));
#if LOOM_ENABLE_JIT
    byteCode->setBase64(utString(json_string_value(json_object_get(classJSON, "bytecode_staticinitializer_fr2"))));
#endif
    type->setBCStaticInitializer(byteCode);


    byteCode = lmNew(NULL) ByteCode();
    byteCode->setBase64(utString(json_string_value(json_object_get(classJSON, "bytecode_instanceinitializer"))));
#if LOOM_ENABLE_JIT
    byteCode->setBase64(utString(json_string_value(json_object_get(classJSON, "bytecode_instanceinitializer_fr2"))));
#endif
    type->setBCInstanceInitializer(byteCode);
}