Example #1
0
// from khtml/ecma/debugger/value2string.cpp
QString valueToString(KJS::JSValue* value)
{
    switch(value->type())
    {
        case KJS::NumberType:
        {
            double v = 0.0;
            value->getNumber(v);
            return QString::number(v);
        }
        case KJS::BooleanType:
            return value->getBoolean() ? "true" : "false";
        case KJS::StringType:
        {
            KJS::UString s;
            value->getString(s);
            return '"' + s.qstring() + '"';
        }
        case KJS::UndefinedType:
            return "undefined";
        case KJS::NullType:
            return "null";
        case KJS::ObjectType:
            return "[object " + static_cast<KJS::JSObject*>(value)->className().qstring() +"]";
        case KJS::GetterSetterType:
        case KJS::UnspecifiedType:
        default:
            return QString();
    }
}
Example #2
0
// Need to support both get and call, so that list[0] and list(0) work.
KJS::JSValue* JSNodeList::callAsFunction(KJS::ExecState* exec, KJS::JSObject* thisObj, const KJS::List& args)
{
    // Do not use thisObj here. See JSHTMLCollection.
    KJS::UString s = args[0]->toString(exec);
    bool ok;
    unsigned u = s.toUInt32(&ok);
    if (ok)
        return toJS(exec, impl()->item(u));

    return KJS::jsUndefined();
}
Example #3
0
PassRefPtr<StringImpl> AtomicString::add(const KJS::UString& ustring)
{
    if (ustring.isNull())
        return 0;

    UString::Rep* string = ustring.rep();
    unsigned length = string->size();
    if (!length)
        return StringImpl::empty();

    HashAndCharacters buffer = { string->hash(), string->data(), length }; 
    pair<HashSet<StringImpl*>::iterator, bool> addResult = stringTable->add<HashAndCharacters, HashAndCharactersTranslator>(buffer);
    if (!addResult.second)
        return *addResult.first;
    return adoptRef(*addResult.first);
}
bool JSConsoleWidget::execute( const QString &cmd, const KJS::Value &self )
{
    KJS::Completion jsres;
    bool ok = js->execute( jsres, cmd, self );

    // Executed ok
    if ( ok ) {

	// No return value
	if ( !jsres.isValueCompletion() )
	    return ok;

	// Return value
	KJS::Value ret = jsres.value();
	KJS::UString s = ret.toString( js->globalExec() );

	if ( s.isNull() ) {
	    warn( i18n("Success, but return value cannot be displayed") );
	    return ok;
	}

	QString txt = s.qstring();
	txt = txt.replace( QChar('\n'), "<br>" );
	println( txt );

	return ok;
    }

    // Handle errors
    KJS::ComplType ct = jsres.complType();
    if ( (ct == KJS::Throw) || (ct == KJS::Break) || ct == KJS::Continue ) {

	KJS::UString s = jsres.value().toString( js->globalExec() );
	if ( !s.isNull() )
	    warn( s.qstring() );
	else
	    warn( i18n("Unspecified error") );
    }
    else {
	kdDebug(80001) << "jsconsolewidget: Unknown completion error, " << ct << endl;
	warn( i18n("Unknown error returned, completion type %1").arg(ct) );
    }

    return ok;
}
Example #5
0
bool KJScript::checkSyntax(const KJS::UString &code )
{
  return rep->evaluate(code.data(), code.size(), 0, true);
}
Example #6
0
bool KJSDebugWin::sourceParsed(KJS::ExecState *exec, int sourceId, const KJS::UString &source, int errorLine)
{
    // Work out which source file this fragment is in
    SourceFile *sourceFile = 0;
    if(!m_nextSourceUrl.isEmpty())
        sourceFile = getSourceFile(exec->interpreter(), m_nextSourceUrl);

    int index;
    if(!sourceFile)
    {
        index = m_sourceSel->count();
        if(!m_nextSourceUrl.isEmpty())
        {

            QString code = source.qstring();
            KParts::ReadOnlyPart *part = static_cast< ScriptInterpreter * >(exec->interpreter())->part();
            if(m_nextSourceUrl == part->url().url())
            {
                // Only store the code here if it's not from the part's html page... in that
                // case we can get it from KHTMLPageCache
                code = QString::null;
            }

            sourceFile = new SourceFile(m_nextSourceUrl, code, exec->interpreter());
            setSourceFile(exec->interpreter(), m_nextSourceUrl, sourceFile);
            m_sourceSelFiles.append(sourceFile);
            m_sourceSel->insertItem(m_nextSourceUrl);
        }
        else
        {
            // Sourced passed from somewhere else (possibly an eval call)... we don't know the url,
            // but we still know the interpreter
            sourceFile = new SourceFile("(unknown)", source.qstring(), exec->interpreter());
            m_sourceSelFiles.append(sourceFile);
            m_sourceSel->insertItem(QString::number(index) += "-???");
        }
    }
    else
    {
        // Ensure that each source file to be displayed is associated with
        // an appropriate interpreter
        if(!sourceFile->interpreter)
            sourceFile->interpreter = exec->interpreter();
        for(index = 0; index < m_sourceSel->count(); index++)
        {
            if(m_sourceSelFiles.at(index) == sourceFile)
                break;
        }
        assert(index < m_sourceSel->count());
    }

    SourceFragment *sf = new SourceFragment(sourceId, m_nextSourceBaseLine, errorLine, sourceFile);
    m_sourceFragments[sourceId] = sf;

    if(m_sourceSel->currentItem() < 0)
        m_sourceSel->setCurrentItem(index);

    if(m_sourceSel->currentItem() == index)
    {
        displaySourceFile(sourceFile, true);
    }

    m_nextSourceBaseLine = 1;
    m_nextSourceUrl = "";

    return (m_mode != Stop);
}
jvalue KJS::Bindings::convertValueToJValue (KJS::ExecState *exec, KJS::Value value, JNIType _JNIType, const char *javaClassName)
{
    jvalue result;
    double d = 0;
   
    d = value.toNumber(exec);
    switch (_JNIType){
        case object_type: {
            result.l = (jobject)0;
            
            // First see if we have a Java instance.
            if (value.type() == KJS::ObjectType){
                KJS::ObjectImp *objectImp = static_cast<KJS::ObjectImp*>(value.imp());
                if (strcmp(objectImp->classInfo()->className, "RuntimeObject") == 0) {
                    KJS::RuntimeObjectImp *imp = static_cast<KJS::RuntimeObjectImp *>(value.imp());
                    JavaInstance *instance = static_cast<JavaInstance*>(imp->getInternalInstance());
                    result.l = instance->javaInstance();
                }
                else if (strcmp(objectImp->classInfo()->className, "RuntimeArray") == 0) {
                    KJS::RuntimeArrayImp *imp = static_cast<KJS::RuntimeArrayImp *>(value.imp());
                    JavaArray *array = static_cast<JavaArray*>(imp->getConcreteArray());
                    result.l = array->javaArray();
                }
            }
            
            // Now convert value to a string if the target type is a java.lang.string.
            if (result.l == 0 && strcmp(javaClassName, "java.lang.String") == 0) {
                KJS::UString stringValue = value.toString(exec);
                JNIEnv *env = getJNIEnv();
                jobject javaString = env->functions->NewString (env, (const jchar *)stringValue.data(), stringValue.size());
                result.l = javaString;
            }
        }
        break;
        
        case boolean_type: {
            result.z = (jboolean)d;
        }
        break;
            
        case byte_type: {
            result.b = (jbyte)d;
        }
        break;
        
        case char_type: {
            result.c = (jchar)d;
        }
        break;

        case short_type: {
            result.s = (jshort)d;
        }
        break;

        case int_type: {
            result.i = (jint)d;
        }
        break;

        case long_type: {
            result.j = (jlong)d;
        }
        break;

        case float_type: {
            result.f = (jfloat)d;
        }
        break;

        case double_type: {
            result.d = (jdouble)d;
        }
        break;
            
        break;

        case invalid_type:
        default:
        case void_type: {
            //bzero (&result, sizeof(jvalue));
            memset(&result, 0, sizeof(jvalue));
        }
        break;
    }
    return result;
}