Exemple #1
0
// wrapSynchronousExpression wraps the given synchronous expression and turns it into a promise.
func (eg *expressionGenerator) wrapSynchronousExpression(syncExpr esbuilder.ExpressionBuilder) esbuilder.ExpressionBuilder {
	// Wrap the expression in a resolve of a promise. We need the function wrapping to ensure
	// that if the expression raises an exception, we can handle that case as well.
	promiseExpr := esbuilder.Snippet(string(codedom.NewPromiseFunction)).Call(
		esbuilder.Function("",
			esbuilder.Identifier("$resolve").Call(syncExpr), "$resolve"))

	resultName := eg.generateUniqueName("$result")
	eg.addAsyncWrapper(promiseExpr, resultName)
	return esbuilder.Identifier(resultName)
}
Exemple #2
0
// generateNullComparisonOperator generates the expression for a null comparison operator.
func (eg *expressionGenerator) generateNullComparisonOperator(compareOp *codedom.BinaryOperationNode, context generationContext) esbuilder.ExpressionBuilder {
	return eg.generateShortCircuiter(
		compareOp.LeftExpr,
		codedom.LiteralValue("null", compareOp.BasisNode()),
		compareOp.RightExpr,
		context,

		func(resultName string, rightSide esbuilder.ExpressionBuilder) esbuilder.ExpressionBuilder {
			return esbuilder.Call(esbuilder.Snippet(string(codedom.NullableComparisonFunction)), esbuilder.Identifier(resultName), rightSide)
		})
}
Exemple #3
0
// generateType generates the given type into ES5.
func (gen *es5generator) generateType(typedef typegraph.TGTypeDecl) esbuilder.SourceBuilder {
	generating := generatingType{typedef, gen}

	switch typedef.TypeKind() {
	case typegraph.ClassType:
		return esbuilder.Template("class", classTemplateStr, generating)

	case typegraph.ImplicitInterfaceType:
		return esbuilder.Template("interface", interfaceTemplateStr, generating)

	case typegraph.NominalType:
		return esbuilder.Template("nominal", nominalTemplateStr, generating)

	case typegraph.StructType:
		return esbuilder.Template("struct", structTemplateStr, generating)

	case typegraph.ExternalInternalType:
		return esbuilder.Snippet("")

	default:
		panic("Unknown typedef kind")
	}
}
Exemple #4
0
// generateRuntimeFunctionCall generates the expression source for a call to a runtime function.
func (eg *expressionGenerator) generateRuntimeFunctionCall(runtimeCall *codedom.RuntimeFunctionCallNode, context generationContext) esbuilder.ExpressionBuilder {
	arguments := eg.generateExpressions(runtimeCall.Arguments, context)
	return esbuilder.Call(esbuilder.Snippet(string(runtimeCall.Function)), arguments...)
}
Exemple #5
0
// generateStaticMemberReference generates the expression for a static reference to a module or type member.
func (eg *expressionGenerator) generateStaticMemberReference(memberReference *codedom.StaticMemberReferenceNode, context generationContext) esbuilder.ExpressionBuilder {
	staticPath := eg.pather.GetStaticMemberPath(memberReference.Member, memberReference.ParentType)
	return esbuilder.Snippet(staticPath)
}
Exemple #6
0
// generateStaticTypeReference generates the expression source for a static type reference.
func (eg *expressionGenerator) generateStaticTypeReference(staticRef *codedom.StaticTypeReferenceNode, context generationContext) esbuilder.ExpressionBuilder {
	return esbuilder.Snippet(eg.pather.GetTypePath(staticRef.Type))
}
Exemple #7
0
// generateTypeLiteral generates the expression source for a type literal.
func (eg *expressionGenerator) generateTypeLiteral(typeLiteral *codedom.TypeLiteralNode, context generationContext) esbuilder.ExpressionBuilder {
	return esbuilder.Snippet(eg.pather.TypeReferenceCall(typeLiteral.TypeRef))
}
Exemple #8
0
// pushSnippet adds source code to the current state.
func (s *state) pushSnippet(source string) {
	s.pushBuilt(esbuilder.Snippet(source))
}