Пример #1
0
/*
 * __PI__: expand the value of PI (3.14...)
 * Our first constant is the __PI__ constant. The following procedure
 * is the callback associated with the __PI__ identifier. That is, when
 * the __PI__ identifier is seen in the running script, this procedure
 * gets called by the underlying Jx9 virtual machine.
 * This procedure is responsible of expanding the constant identifier to
 * the desired value (3.14 in our case).
 */
static void PI_Constant(
    unqlite_value *pValue, /* Store expanded value here */
    void *pUserData    /* User private data (unused in our case) */
) {
    /* Expand the value of PI */
    unqlite_value_double(pValue, 3.1415926535898);
}
Пример #2
0
unqlite_value* VirtualMachine::createUnqlite(const QVariant& var)
{
	unqlite_value* retval = NULL;
	QVariant::Type type = var.type();
	if (type == QVariant::Map) {
		retval = unqlite_vm_new_array(m_pVm);
		QMap<QString, QVariant> map = var.toMap();
		QMapIterator<QString, QVariant> it(map);
		while (it.hasNext()) {
			it.next();						
			unqlite_value* pVal = createUnqlite(it.value());
			if (pVal) {			
				QString key = it.key();
				const char* pKey = key.toUtf8().data();	
				unqlite_array_add_strkey_elem(retval, pKey, pVal);
				unqlite_vm_release_value(m_pVm, pVal);				
			}
			else {
				qDebug() << "Could not create unqlite value for map";
			}
		}
	}
	else if (type == QVariant::List) {
		retval = unqlite_vm_new_array(m_pVm);
		QList<QVariant> list = var.toList();
		QListIterator<QVariant> it(list);
		while (it.hasNext()) {
			unqlite_value* pVal = createUnqlite(it.next());
			if (pVal) {
				unqlite_array_add_elem(retval, NULL, pVal);
				unqlite_vm_release_value(m_pVm, pVal);
			}
			else {
				qDebug() << "Could not create unqlite value for list";
			}
		}
	}
	else {
		retval = unqlite_vm_new_scalar(m_pVm);
		if (type == QVariant::Int || type == QVariant::UInt) {
			unqlite_value_int(retval, var.toInt());
		}
		else if (type == QVariant::Double) {
			unqlite_value_double(retval, var.toDouble());
		}
		else if (type == QVariant::Bool) {
			unqlite_value_bool(retval, var.toBool());
		}
		else if (type == QVariant::String) {
			QByteArray bytes = var.toString().toUtf8();
			unqlite_value_string(retval, bytes.data(), bytes.length());
		}
		else {
			qDebug() << "Could not determine type of the given QVariant" << var;
		}
	}
	return retval;
}